I was reading this nice blog from A Williams:

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.

有帮助吗?

解决方案

Yes, it does have the mutex locked. Basically, the mutex gets released only while the thread is blocked in 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():

Postcondition:

lock is locked by the current thread.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top