boost::condition_variable cond;
boost::mutex mut;
bool ready = false;
void consumer() {
boost::mutex::scoped_lock lock(mut);
while (!ready) {
cond.wait(lock);
}
}
void producer() {
boost::mutex::scoped_lock lock(mut);
ready = true;
cond.notify_all();
boost::this_thread::sleep(boost::posix_time::seconds(4));
}
Refer to the above code, I actually sleep the producer thread for 4 seconds after I call notify_all(). However, the consumer threads are actually woken up after 4 seconds. So how can I get around this and wake up the consumer threads immediately after I call notify_all() despite the 4 seconds sleep. Thanks in advance.
It has to do with the boost::mutex::scoped_lock lock(mut); in producer.
As the scope ends AFTER the sleep, the mutex is only released after it.
Try with that, if you want to keep your scoped_lock.