我在看这个
A Williams的好博客:

http://www.justsoftwaresolutions.co.uk/threading/condition-variable-spurious-wakes.html

一件事困扰我:
当线程由于虚假唤醒而唤醒时,是否已锁定互斥锁?

boost::mutex::scoped_lock lock(the_mutex);
while(the_queue.empty())
{
    the_condition_variable.wait(lock);
}

我猜是这样,因为否则调用.empty是不安全的,但我不确定。

最佳答案

是的,它确实已锁定了互斥锁。基本上,只有在the_condition_variable.wait()中阻塞线程时才释放互斥量。无论是否伪造的唤醒,该互斥锁都将在您显示的代码中的任何其他位置锁定。
documentation中获取boost::condition_variable::wait():

关于c++ - 虚假唤醒和条件变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15589945/

10-13 06:16