Bugzilla will be upgraded to version 5.0 on a still to be determined date in the near future. The original upgrade date has been delayed.
Bug 61486 - std::string not thread safe
std::string not thread safe
Product: Red Hat Linux
Classification: Retired
Component: libstdc++ (Show other bugs)
i386 Linux
medium Severity high
: ---
: ---
Assigned To: Jakub Jelinek
Depends On:
  Show dependency treegraph
Reported: 2002-03-20 10:04 EST by André Johansen
Modified: 2007-04-18 12:41 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2004-10-01 19:54:05 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)
Test program (882 bytes, text/plain)
2002-03-20 10:06 EST, André Johansen
no flags Details
Patch adding locking for i386 machines (857 bytes, patch)
2002-03-20 10:09 EST, André Johansen
no flags Details | Diff

  None (edit)
Description André Johansen 2002-03-20 10:04:48 EST
The std::string implementation that comes with libstdc++ 2.96-98 is not
thread safe.  The problem will result in a corrupted memory heap.

The reason for the problem is improper locking of the reference count in

The problem is also in the version that comes with GCC v2.95.3, but seems to
be fixed in GCC v3.0.4.  If STLport is used as the standard library, the
problem disappears.

I have a test-program that will trigger this:
[andrej@hermes tests]$ g++ -Wall -O3 -pthread string_mt.cpp -lstdc++ -lpthread
[andrej@hermes tests]$ ./a.out
Thread started (1026)
Thread started (2051)
Thread started (3076)
Segmentation fault
Comment 1 André Johansen 2002-03-20 10:06:51 EST
Created attachment 49145 [details]
Test program
Comment 2 André Johansen 2002-03-20 10:09:06 EST
Created attachment 49146 [details]
Patch adding locking for i386 machines
Comment 3 André Johansen 2002-03-26 05:15:07 EST
Notice that all threaded applications, and all libraries using std::string
that can be used in threaded applications must be recompiled.

Comment 4 Need Real Name 2002-07-29 17:07:17 EDT
To restate the severity of this bug -- std::string is worse than just not 
threadsafe, it is completely unsuitable for any program or library compiled for 
multithreading and run on an SMP iA32 system. You can crash in nilRep() by just 
creating empty strings on the stack, as the static nil representation will have 
its reference count go to zero and be freed. 

Calling a class threadsafe typically implies that the same instance can be 
operated upon by different threads. The current implementation of std::string 
does not allow different instances to be operated on by different threads.
Comment 5 André Johansen 2002-11-06 05:14:39 EST
This problem is still present in Red Hat Linux v7.3. 
Comment 6 Benjamin Kosnik 2004-10-01 19:54:05 EDT
Fixed gcc-3.2.x, gcc-3.3.x, gcc-3.4.x.

Note You need to log in before you can comment on or make changes to this bug.