
I'm having a deadlock problem with boost::condition variables and I'm not sure if I'm miss using condition variables or if condition variables aren't the right tool for the job. The basic problem appears to be that the thread calling notify_all (lets call it thread1) calls notify_all before the thread calling wait (lets call it thread2) calls wait. The result is thread2 waits forever for the notify that thread1 already sent. To further complicate the matter thread1 can't call notify_all again until it has been signaled to run (via a different condition variable) which is signaled by thread2. Does this make sense? Here's some pseudo code for what I'm doing. Thread1() //Worker thread { while(1) { DoSomeStuff(); condition1.notify_all() scoped_lock l(mutex2); condition2.wait(l); } } Thread2() //Master thread. { while(1) { scoped_lock l(mutex1); condition1.wait(l) DoSomeOtherStuff(); condition2.notify_all(); } } StartMulitThreadedTask() { startThread( thread2 ); Sleep( 2000 ) startThread( thread1 ); } Here is a little time line of what I think is happening thread2 locks mutex1 thread2 calls wait on condition1 thread1 calls DoSomeStuff thread1 calls notify_all on condition1 thread1 locks mutex2 thread1 calls wait on condition2 thread2 calls DoSomeOtherStuff thread2 calls notify_all on condition2 thread1 calls DoSomeStuff thread1 calls notify_all on condition1 thread1 locks mutex2 thread1 calls wait on condition1 thread2 locks mutex1 thread2 calls wait on condition1 DEADLOCK OCCURS I've done this sort of thing with plain old binary mutexes or single count semaphores and I can go back to that but it really seemed like boost::conditions simplified things a bit and should be right choice except for this one problem. Is there something I can do to make boost::conditions work in this case? Should I be doing additional or different locking of mutex1 and mutex2? do I need yet another mutex? Thanks Matt S.