From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6892 invoked by alias); 2 Jul 2012 16:21:10 -0000 Received: (qmail 6876 invoked by uid 22791); 2 Jul 2012 16:21:06 -0000 X-SWARE-Spam-Status: No, hits=-3.6 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 02 Jul 2012 16:20:53 +0000 From: "m_reicha at informatik dot uni-kl.de" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/53830] New: condition_variable_any - deadlock issue Date: Mon, 02 Jul 2012 16:21:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: m_reicha at informatik dot uni-kl.de X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2012-07/txt/msg00281.txt.bz2 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: unassigned@gcc.gnu.org ReportedBy: m_reicha@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 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 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.