http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53830
Bug #: 53830 Summary: condition_variable_any - deadlock issue Classification: Unclassified Product: gcc Version: 4.6.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: m_rei...@informatik.uni-kl.de Created attachment 27730 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=27730 Simple test program that causes deadlock I believe there is a bug (or a pitfall at least) in std::condition_variable_any that can cause deadlocks. Actually, one of my programs just froze because of this. I am using gcc 4.6.3. However, I believe the bug is also in the latest headers in the CVS (trunk). A common use case for condition_variables, is something like this: std::mutex mutex; std::condition_variable_any cv; // called by thread#1: waits for data from another thread void wait_for_data() { std::unique_lock<std::mutex> lock(mutex); cv.wait_for(lock, std::chrono::seconds(2)); // no predicate for simplicity // dequeue data } // called by thread#2: passes data to waiting thread void provide_data() { std::unique_lock<std::mutex> lock(mutex); // enqueue data cv.notify_one(); } If thread#1's timeout expires while thread#2 already holds the lock on "mutex", this will deadlock. This is because condition_variable_any uses another internal mutex, which is usually acquired after "mutex". However, if the timeout expires, the internal mutex is acquired before "mutex". By adding a nonsense "sleep_for", we can actually make a simple test program always deadlocks (see attachment). Notably, the same test program does not deadlock, if a boost::condition_variable_any or a std::condition_variable is used instead of a std::condition_variable_any.