为什么lock_guard可以通过unique_lock获得一个已经锁定的互斥量

why lock_guard can get an already locked mutex by unique_lock?

本文关键字:lock 一个 锁定 unique 可以通过 guard 为什么      更新时间:2023-10-16

我在这里阅读使用condition_variable的示例代码。我把代码贴在下面:

std::mutex m;
std::condition_variable cv;
std::string data;
bool ready = false;
bool processed = false;
void worker_thread()
{
    // Wait until main() sends data
    std::cout << "------------------------n";
    std::unique_lock<std::mutex> lk(m);
    cv.wait(lk, []{return ready;});
    // after the wait, we own the lock.
    std::cout << "Worker thread is processing datan";
    data += " after processing";
    // Send data back to main()
    processed = true;
    std::cout << "Worker thread signals data processing completedn";
    // Manual unlocking is done before notifying, to avoid waking up
    // the waiting thread only to block again (see notify_one for details)
    lk.unlock();
    cv.notify_one();
}
int main()
{
    std::thread worker(worker_thread);
    data = "Example data";
    // send data to the worker thread
    {
        std::lock_guard<std::mutex> lk(m);
        ready = true;
        std::cout << "main() signals data ready for processingn";
    }
    cv.notify_one();
    // wait for the worker
    {
        std::unique_lock<std::mutex> lk(m);
        cv.wait(lk, []{return processed;});
    }
    std::cout << "Back in main(), data = " << data << 'n';
    worker.join();
    return 0;
}

我的问题是worker_thread首先启动,所以我假设互斥锁mworker_thread锁定,但为什么在main互斥锁m仍然可以被lock_guard锁定?

条件变量只是三脚架的一部分。

这三部分是条件变量、状态和保护状态的互斥锁。

条件变量提供了一种机制,当状态发生变化时通知。

此操作使用所有3:

cv.wait(lk, []{return ready;})

条件变量的方法接受一个锁(必须已获得)和一个lambda(用于测试状态)。

wait方法中,lk解锁,直到条件变量检测到消息(可能是假的)。当它检测到消息时,它将重新锁定互斥锁并运行测试(其目标是确定检测是否为伪造)。如果测试失败,它将解锁并再次等待;如果测试通过,它将保持锁定并退出。

还有"the test thrown"路径,它会导致不同的锁状态,这取决于代码实现的标准版本(c++ 11有一个缺陷,IIRC)。

您错过的重要事情是wait解锁了传入的互斥锁。