为什么lock_guard可以通过unique_lock获得一个已经锁定的互斥量
why lock_guard can get an already locked mutex by unique_lock?
我在这里阅读使用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
首先启动,所以我假设互斥锁m
被worker_thread
锁定,但为什么在main
互斥锁m
仍然可以被lock_guard
锁定?
条件变量只是三脚架的一部分。
这三部分是条件变量、状态和保护状态的互斥锁。
条件变量提供了一种机制,当状态发生变化时通知。
此操作使用所有3:
cv.wait(lk, []{return ready;})
条件变量的方法接受一个锁(必须已获得)和一个lambda(用于测试状态)。
在wait
方法中,lk
将解锁,直到条件变量检测到消息(可能是假的)。当它检测到消息时,它将重新锁定互斥锁并运行测试(其目标是确定检测是否为伪造)。如果测试失败,它将解锁并再次等待;如果测试通过,它将保持锁定并退出。
还有"the test thrown"路径,它会导致不同的锁状态,这取决于代码实现的标准版本(c++ 11有一个缺陷,IIRC)。
您错过的重要事情是wait
解锁了传入的互斥锁。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 从链接列表c++中删除一个项目
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '