
On 4/19/07, Peter Dimov
PJ Durai wrote:
On 4/18/07, Ken
wrote: On Apr 13, 2007, PJ Durai wrote:
I have two threads waiting on a condition. When I call condition::notify_all() from the main thread, I expected both threads to fall through and proceed simultaneously.
But that is not what I am witnessing. They behave like I had called condition::notify_one() twice.
One thread continues, does its thing (in this case a simple Sleep and a printf) comes back to wait. Only after that point the second thread continued from the condition.
Is this expected ? What am I missing ?
This is expected. condition::wait reacquires the mutex after it returns. You then call Sleep(4000) with the mutex locked; the other threads can't continue because they are waiting for the mutex. Try unlocking lk before Sleep'ing.
Thanks peter. That fixed it. My loop looks like this now. And it is working as expected. void operator() () { while(1) { { TRACE("%s : waiting ...\n", name.c_str()); { TRACE("%s : before cond wait...\n", name.c_str()); { boost::mutex::scoped_lock lk(mutex); cond.wait(lk); } TRACE("%s : after cond wait...\n", name.c_str()); { TRACE("%s : doing stuff...\n", name.c_str()); //ElapsedTime etime(name.c_str()); Sleep(4000); for (int i=0; i<10; ++i) std::cout << id; std::cout << std::endl; } } } //barrier.wait(); } } Thanks Guys.