public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/53830] New: condition_variable_any - deadlock issue
@ 2012-07-02 16:21 m_reicha at informatik dot uni-kl.de
  2012-07-02 16:54 ` [Bug libstdc++/53830] " redi at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: m_reicha at informatik dot uni-kl.de @ 2012-07-02 16:21 UTC (permalink / raw)
  To: gcc-bugs

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<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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-07-05  1:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-02 16:21 [Bug libstdc++/53830] New: condition_variable_any - deadlock issue m_reicha at informatik dot uni-kl.de
2012-07-02 16:54 ` [Bug libstdc++/53830] " redi at gcc dot gnu.org
2012-07-02 17:25 ` redi at gcc dot gnu.org
2012-07-04 22:17 ` redi at gcc dot gnu.org
2012-07-05  0:19 ` redi at gcc dot gnu.org
2012-07-05  1:10 ` redi at gcc dot gnu.org
2012-07-05  1:16 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).