Compiling with: g++ -g -O3 gccbug.cpp -pthread -o gccbug -s Note that removing the -s eliminates the segfault, as does removing optimizations with -O0.
This occurs with gcc 3.3 and 3.3.6 but does not occur with the gcc 3.2.3 delivered as part of RedHat ES3.0u5. It is also fixed in 3.4.6. #include <pthread.h> #include <string> void* thread_function(void*) { for (int k = 0; k < 50000; k++) { std::string my_str; my_str += "foo"; } return 0; } int main() { pthread_t thread1, thread2; pthread_create(&thread1, NULL, thread_function, NULL); pthread_create(&thread2, NULL, thread_function, NULL); void* exitcode; pthread_join(thread1, &exitcode); pthread_join(thread2, &exitcode); return 0; } Example stack trace (as a courtesy to future googlers looking for this issue): #0 0x00d3d4cc in memcpy () from /lib/libc.so.6 #1 0x00ba9120 in std::string::_Rep::_M_clone (this=0x804a700, [EMAIL PROTECTED], __res=28) at /build_gnu_build/gcc-3.3.6/i386-redhat-linux/libstdc++-v3/include/bits/char_traits.h:155 #2 0x00ba7519 in std::string::reserve (this=0xb6bf43b0, __res=28) at /build_gnu_build/gcc-3.3.6/i386-redhat-linux/libstdc++-v3/include/bits/basic_string.h:257 #3 0x00ba77be in std::string::append (this=0xb6bf43b0, [EMAIL PROTECTED]) at /build_gnu_build/gcc-3.3.6/i386-redhat-linux/libstdc++-v3/include/bits/basic_string.tcc:680 #4 0x08048c21 in ?? () #5 0x080490da in ?? () #6 0x08049093 in ?? () #7 0x08048fa6 in ?? () #8 0x003ef9d9 in start_thread (arg=0xb6bf4bb0) at pthread_create.c:261 #9 0x00d86f0e in clone () from /lib/libc.so.6 -- Summary: Thread race segfault in std::string::append with -O and -s Product: gcc Version: 3.3.6 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: appfault at hotmail dot com GCC host triplet: 2.4.21-32.ELsmp #1 SMP Fri Apr 15 21:17:59 EDT 2005 i686 i686 i3 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32261