A problem with STL vector/list/map with string as a key in multi-threaded program

This is a discussion on A problem with STL vector/list/map with string as a key in multi-threaded program within the Linux Programming forums, part of the Platform Specific Boards category; Hi, While developing a multi-threaded application I've encountered a problem that is reproduced in a testcase presented below. The purpose ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    A problem with STL vector/list/map with string as a key in multi-threaded program

    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:
    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;
    }
    The compiler I'm using is: gcc version 4.0.0 20050519 (Red Hat 4.0.0-8) (i386-redhat-linux).
    I will really appreciate any help on the subject

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    The really weird part is that your threads don't even run in parallel. So no threading bug there, really.

    I tried the example. Had to correct the 64-bit incompatibility first (GCC gives you an error if you try to cast a 64-bit pointer to a int - interesting). I can't reproduce the crash with GCC 4.1.2 (Gentoo).

    4.0.0, being the first of the 4 series, is known to have quite a few problems, though. You should really upgrade to 4.2 (very stable) or 4.3 (stable release, but young).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    My first guess is that you are running out of stack space for all those threads. 4097? That's suspiciously close to 4096, isn't it?

    Removing the std::string variable reduces the need to use stack. I bet you're right on the edge.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    First, in that case pthread_create should return EAGAIN instead of crash.
    Second, there's never more than two threads active. pthread_join waits for the thread to finish.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    2
    Thanks for the responses,
    Upgrading the gcc to 4.0.2 solved the problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-14-2008, 06:17 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. C/C++ String Problem.
    By Jaken Veina in forum C++ Programming
    Replies: 7
    Last Post: 07-09-2005, 10:11 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21