Hi,
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).Code:#include <iostream> #include <string> #include <pthread.h> #include <stdio.h> #include <map> #include <vector> #include <list> using namespace std; void* f(void *arg) { int i = (int)arg; string str = "foobar"; // <-- segmentation fault cout << "finished:"<< i << endl; return 0; } void mt_test() { string str1("f"); map<string, string> myMap; myMap[str1] = "akjshd"; // <-- if removed, no crash const int NUM_THREADS = 6000; void* ret=0; for(int i=0;i<NUM_THREADS;++i) { pthread_t thread; int retVal = pthread_create(&thread, NULL, f, (void*)i); if (retVal != 0) { cout << "error: " << retVal << endl; exit(-1); } pthread_join(thread, &ret); } } int main(int argc, char *argv[]) { mt_test(); return EXIT_SUCCESS; }
I will really appreciate any help on the subject![]()



LinkBack URL
About LinkBacks




CornedBee