[英]Spurios wake up and condition variables
I was reading this nice blog from A Williams: 我从A Williams读了一个不错的博客:
http://www.justsoftwaresolutions.co.uk/threading/condition-variable-spurious-wakes.html http://www.justsoftwaresolutions.co.uk/threading/condition-variable-spurious-wakes.html
and one thing bugs me: when thread wakes up because of the spurious wake does it have mutex locked? 一件事困扰着我:当线程由于虚假唤醒而唤醒时,它是否已锁定互斥锁?
boost::mutex::scoped_lock lock(the_mutex);
while(the_queue.empty())
{
the_condition_variable.wait(lock);
}
I guess so because otherwise call to .empty would be unsafe but Im not sure. 我猜是这样,因为否则调用.empty是不安全的,但我不确定。
Yes, it does have the mutex locked. 是的,它确实已锁定了互斥锁。 Basically, the mutex gets released only while the thread is blocked in
the_condition_variable.wait()
. 基本上,互斥锁仅在线程在
the_condition_variable.wait()
被阻塞时才释放。 Spurious wakeup or not, the mutex is locked everywhere else in the code that you show. 不管是否是虚假唤醒,互斥锁都将在显示的代码中锁定在其他任何地方。
From the documentation for boost::condition_variable::wait()
: 从
boost::condition_variable::wait()
的文档中:
Postcondition:
后置条件:
lock
is locked by the current thread.lock
被当前线程锁定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.