【问题标题】:do condition variables unlock their mutex?条件变量会解锁它们的互斥锁吗?
【发布时间】:2015-07-23 04:15:05
【问题描述】:

我正在处理条件变量,我假设它们在等待时解锁了相关的互斥锁。否则,互斥锁将永远不会被释放。但是,我在任何文档中都找不到此信息。考虑以下代码:

std::condition_variable consumerWakeMeUp;
std::mutex queueMutex;
// this locks the mutex
std::unique_lock<std::mutex> lk(queueMutex);
// going to sleep now
consumerWakeMeUp.wait(lk);

“consumerWakeMeUp.wait(lk)”是否解锁互斥锁?我必须假设否则线程将永远处理该互斥体。但如果有人知道更多细节,我将不胜感激。

谢谢。

【问题讨论】:

  • Yet, I can't find this information on any documentation 实际上是第 1 点 here

标签: c++ multithreading stl thread-safety


【解决方案1】:

没关系

"原子释放锁,阻塞当前执行线程,并将其添加到等待*this的线程列表中。执行notify_all()或notify_one()时,线程会被解除阻塞,也可能是虚假解除阻塞。 解除阻塞时,无论什么原因,都会重新获取锁并等待退出。如果此函数因异常退出,也会重新获取锁。(C++14前)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-06
    • 2013-12-17
    • 1970-01-01
    • 2018-05-23
    • 2021-10-16
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多