Thread safety of basic_string

Dan McGuirk mcguirk@incompleteness.net
Tue Nov 13 15:55:00 GMT 2001


Hi,

I'm trying to debug a heavily multi-threaded application that does a lot
of constructing, concatenating, and destroying of std::string objects. 
I'm running the code under Solaris 2.8, built with gcc 3.0.2 with
--enable-threads=posix.

I seem to be running into thread safety issues with basic_string.  Under
heavy load, the application crashes after several hours, and according
to the cores appears to be trying to use some bogus strings (bad
lengths, contents have already been freed, etc.)

I've been reading through the FAQ and the list archives and am coming up
to speed on what is and is not safe to do with std::string (and other
stdc++ classes) in a threaded program.  I understand that you should not
access the same string from multiple threads without locking.  The first
problem reported in a Purify run seemed to be related to this-- a string
that was illegitimately shared this way wound up being disposed of while
there was still a reference to it.  However, I fixed this problem
wherever I could find it, and I still wound up with problems related to
_S_empty_rep_storage being disposed of due to a zero reference count.

So my question is, what can I do to fix this application, which
obviously expects more thread safety than std::string currently
provides?  Is ensuring that no single string is ever shared between
threads enough, or will I always have problems related to
_S_empty_rep_storage no matter what I do?

I am willing to put work into improving the thread safety of std::string
if someone can give me some hints on what needs to be done.  I am even
open to platform-specific hacks that will get me up and running in my
environment, although of course more generally useful improvements that
can be contributed back would be even better.

Thanks for any help, and my apologies if I'm suffering from any basic
misunderstandings.  Let me know if there is more useful information I
can provide.



More information about the Libstdc++ mailing list