While developing a multi-threaded application I've encountered a problem that is reproduced in a testcase presented below.
The purpose of the sample is to create a large number of threads where each thread uses stl strings. The threads execute consecutively and not in parallel.
The problem is that the program crashes with a segmentation fault at line 14 (string instantiation in the f function) in thread number 4095 which is the 4097-th created thread (including the main thread). The segmentation fault doesn't happen if line 23 is removed (assignment of a value into std::map<string, string> in mt_test function). The problem is also reproduced if the insertion into map is replaced with insertion into a std::vector<std::string> or std::list<std::string>.
The source code:
The compiler I'm using is: gcc version 4.0.0 20050519 (Red Hat 4.0.0-8) (i386-redhat-linux).
using namespace std;
void* f(void *arg)
int i = (int)arg;
string str = "foobar"; // <-- segmentation fault
cout << "finished:"<< i << endl;
map<string, string> myMap;
myMap[str1] = "akjshd"; // <-- if removed, no crash
const int NUM_THREADS = 6000;
int retVal = pthread_create(&thread, NULL, f, (void*)i);
if (retVal != 0)
cout << "error: " << retVal << endl;
int main(int argc, char *argv)
I will really appreciate any help on the subject