Pregunta

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.

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top