02.02.2010

Spurious wakeup

There are some freaky things in multithreads programming. One of the is so called "Spurious wakeup".
So, seemly valid code:

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

need to be corrected:

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

links: