Re: [sigc] Re: [gtkmm] libsigcx and gtkmm 2.4



> m... after reading:
>
>  http://gcc.gnu.org/onlinedocs/libstdc++/faq/#5_6
>
>I still cannot answer the question whether string (in the example from  
>above) _is_ thread-safe?!

std::string can never be considered thread safe.

it is possible that some specific implementations of std::string are
thread safe, either by design or by accident, but you should never
assume this.

std::string instances may share references to a common data
buffer. multiple threads may be operating on multiple instances of
std::string's which happen to share the data buffer. there is no
interlocking, no mutexes and absolutely no memory barrier that
controls these interactions. consider:

	 std::string foo = "foobar;
	 std::string bar = foo;

	 // foo and bar both use the same data buffer
	 
	 thread 1                   thread 2
	 ========		    ========
	 foo[0] = 's';
	 // buffer holds soobar
	                            if (bar[0] == 'f') 
				    

instant race condition if the implementation of std::string is devoid
of mutexes. depending on the execution order of the two threads,
thread 2 may mor may take a particular branch based on the first
character of the string.

designing thread safe data structures is tricky. std::string has never
been intended to have these semantics, and its quite difficult to add
them. the simplest approach is to use mutex protected copy-on-write,
but even that has complications.

if you need a thread safe string class, you will need to implement
your own or find one on the net (i have never looked, but i imagine
one might exist).

--p



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]