public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/39909]  New: non-TLS version of std::call_once causes terminate
@ 2009-04-26 13:51 jwakely dot gcc at gmail dot com
  2009-04-26 14:28 ` [Bug libstdc++/39909] " paolo dot carlini at oracle dot com
                   ` (9 more replies)
  0 siblings, 10 replies; 12+ messages in thread
From: jwakely dot gcc at gmail dot com @ 2009-04-26 13:51 UTC (permalink / raw)
  To: gcc-bugs

I'm entering this bug to track:
http://gcc.gnu.org/ml/libstdc++/2009-04/msg00009.html

The following program fails using gcc configured --disable-tls

#include <mutex>
#include <thread>
#include <assert.h>

std::once_flag flag;
int value = 0;

struct Inc { void operator()() const { ++value; } };

struct Func
{
   void operator()() const
   {
       Inc inc;
       for (int i = 0; i < 10000;  ++i)
           std::call_once(flag, inc);
   }
};

int main()
{
   Func f;
   std::thread t1(f);
   std::thread t2(f);
   std::thread t3(f);
   t1.join();
   t2.join();
   t3.join();
   assert( value == 1 );
   return 0;
}

Multiple threads try to call unique_lock<mutex>::lock() on the same object
simultaneously, after the first one acquires the lock the subsequent threads
will fail due to this check in unique_lock::lock()

        else if (_M_owns)
          __throw_system_error(int(errc::resource_deadlock_would_occur));

The unhandled exceptions terminate the program.

The obvious fix is to stop using a single, global lock object shared between
all threads, and use a different lock object in each thread's stack, sharing a
global mutex.  Concurrent operations on a single mutex are safe, but not on a
single lock.

That fix requires an ABI change, since __get_once_functor_lock is exported.  An
alternative would be to replicate the code used when TLS is available, but
using pthread_getspecific / pthread_setspecific instead.


-- 
           Summary: non-TLS version of std::call_once causes terminate
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jwakely dot gcc at gmail dot com


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


^ permalink raw reply	[flat|nested] 12+ messages in thread
[parent not found: <bug-39909-4@http.gcc.gnu.org/bugzilla/>]

end of thread, other threads:[~2020-11-03 19:35 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-26 13:51 [Bug libstdc++/39909] New: non-TLS version of std::call_once causes terminate jwakely dot gcc at gmail dot com
2009-04-26 14:28 ` [Bug libstdc++/39909] " paolo dot carlini at oracle dot com
2009-04-26 17:40 ` jwakely dot gcc at gmail dot com
2009-04-26 17:53 ` paolo dot carlini at oracle dot com
2009-04-26 17:53 ` paolo dot carlini at oracle dot com
2009-04-26 18:06 ` jwakely dot gcc at gmail dot com
2009-04-26 21:39 ` jwakely dot gcc at gmail dot com
2009-04-29  9:18 ` paolo dot carlini at oracle dot com
2009-05-05 21:33 ` redi at gcc dot gnu dot org
2009-05-05 21:44 ` redi at gcc dot gnu dot org
2009-05-05 22:14 ` jwakely dot gcc at gmail dot com
     [not found] <bug-39909-4@http.gcc.gnu.org/bugzilla/>
2020-11-03 19:35 ` 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).