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

* [Bug libstdc++/53830] condition_variable_any - deadlock issue
  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 ` redi at gcc dot gnu.org
  2012-07-02 17:25 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2012-07-02 16:54 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53830

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2012-07-02
   Target Milestone|---                         |4.6.4
     Ever Confirmed|0                           |1

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-07-02 16:53:47 UTC ---
confirmed


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

* [Bug libstdc++/53830] condition_variable_any - deadlock issue
  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
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2012-07-02 17:25 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53830

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
         AssignedTo|unassigned at gcc dot       |redi at gcc dot gnu.org
                   |gnu.org                     |

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-07-02 17:25:16 UTC ---
similar issue to PR 50862, I should have fixed it at the same time


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

* [Bug libstdc++/53830] condition_variable_any - deadlock issue
  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
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2012-07-04 22:17 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53830

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-07-04 22:17:24 UTC ---
Author: redi
Date: Wed Jul  4 22:17:18 2012
New Revision: 189268

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=189268
Log:
    PR libstdc++/53830
    * include/std/condition_variable (condition_variable_any::wait):
    Move _Unlock type to class scope.
    (condition_variable_any::wait_until): Reuse it.
    * testsuite/30_threads/condition_variable_any/53830.cc: New.

Added:
    trunk/libstdc++-v3/testsuite/30_threads/condition_variable_any/53830.cc
Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/std/condition_variable


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

* [Bug libstdc++/53830] condition_variable_any - deadlock issue
  2012-07-02 16:21 [Bug libstdc++/53830] New: condition_variable_any - deadlock issue m_reicha at informatik dot uni-kl.de
                   ` (2 preceding siblings ...)
  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
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2012-07-05  0:19 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53830

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-07-05 00:19:41 UTC ---
Author: redi
Date: Thu Jul  5 00:19:36 2012
New Revision: 189273

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=189273
Log:
    PR libstdc++/53830
    * include/std/condition_variable (condition_variable_any::wait):
    Move _Unlock type to class scope.
    (condition_variable_any::wait_until): Reuse it.
    * testsuite/30_threads/condition_variable_any/53830.cc: New.

Added:
   
branches/gcc-4_7-branch/libstdc++-v3/testsuite/30_threads/condition_variable_any/53830.cc
Modified:
    branches/gcc-4_7-branch/libstdc++-v3/ChangeLog
    branches/gcc-4_7-branch/libstdc++-v3/include/std/condition_variable


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

* [Bug libstdc++/53830] condition_variable_any - deadlock issue
  2012-07-02 16:21 [Bug libstdc++/53830] New: condition_variable_any - deadlock issue m_reicha at informatik dot uni-kl.de
                   ` (3 preceding siblings ...)
  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
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2012-07-05  1:10 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53830

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-07-05 01:10:18 UTC ---
Author: redi
Date: Thu Jul  5 01:10:10 2012
New Revision: 189275

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=189275
Log:
    PR libstdc++/53830
    * include/std/condition_variable (condition_variable_any::wait):
    Move _Unlock type to class scope.
    (condition_variable_any::wait_until): Reuse it.
    * testsuite/30_threads/condition_variable_any/53830.cc: New.

Added:
   
branches/gcc-4_6-branch/libstdc++-v3/testsuite/30_threads/condition_variable_any/53830.cc
Modified:
    branches/gcc-4_6-branch/libstdc++-v3/ChangeLog
    branches/gcc-4_6-branch/libstdc++-v3/include/std/condition_variable


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

* [Bug libstdc++/53830] condition_variable_any - deadlock issue
  2012-07-02 16:21 [Bug libstdc++/53830] New: condition_variable_any - deadlock issue m_reicha at informatik dot uni-kl.de
                   ` (4 preceding siblings ...)
  2012-07-05  1:10 ` redi at gcc dot gnu.org
@ 2012-07-05  1:16 ` redi at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2012-07-05  1:16 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53830

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED
      Known to fail|                            |4.6.3, 4.7.1

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-07-05 01:16:28 UTC ---
Fixed for 4.6.4, 4.7.2 and 4.8.0


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