Yet a question about g++/STL/multithreading

PeterPan peterpan@chinaren-inc.com
Wed Apr 25 06:42:00 GMT 2001


Hi,
    I've been using g++ to compile a multithreading program on Solaris x86, and my program crashed when I run it. The detail was much like what Nafees Qureshy wrote in http://mail.gnu.org/pipermail/help-gplusplus/2000-April/000223.html except that I used 'list' containers.
    In order to discribe the problem of me, I tried to make some change to the example Nafees Qureshy gave. Yes,it worked(I meant 'core dumped').
                                                                  yours, Xuesong Guo
                                                                  2001.4.22
 
peterpan@mailsrv0 ~$ uname -a
SunOS mailsrv0 5.8 Generic_108529-06 i86pc i386 i86pc
peterpan@mailsrv0 ~$ g++ -v
Reading specs from /export/home/peterpan/lib/gcc-lib/i386-pc-solaris2.8/2.95.2/specs
gcc version 2.95.2 19991024 (release)
peterpan@mailsrv0 ~$ cat t.cc
#include <iostream.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <map>
#include <list>
#include <string>


using namespace std;


typedef map<string, string> map_t;


class MyObject {
public:
   int aNumber;
   map<string, string> aMap;
   list<string> aList;

   MyObject(int n, map<string, string> m,list<string>l) {
      aNumber = n;
      aMap = m;
      aList = l;
   }
};


extern "C" void* newThread(void* aArg) {

    int num_objects = (int)aArg;

    int i;

    map<string, string> aMap;
    list<string> aList;
/*    aMap["k1"] = "v1";
    aMap["k2"] = "v2";
    aMap["k3"] = "v3";*/

    aList.push_back("v1");
    aList.push_back("v2");
    aList.push_back("v3");

    for (i = 0; i < num_objects; ++i) {
        MyObject* m = new MyObject(i, aMap,aList);
        delete m;
    }

    return NULL;
}


int main(int argc, char **argv) {

    int num_threads = 0;
    int num_objects = 0;
    int i;

    if (argc != 3) {
        cout <<
            "USAGE:  main <number of threads> <number of objects to
allocate>"
            << endl;
        return 0;
    }

    num_threads = atoi(argv[1]);
    num_objects = atoi(argv[2]);

    pthread_t* threadArray = new pthread_t[num_threads];

    for (i = 0; i < num_threads; ++i) {
        cout<<"***** Creating thread number: "<< i+1 <<endl;
        if (pthread_create(&threadArray[i], 0, newThread,
            (void*)num_objects) != 0) {
            cout<<"Error creating thread "<<endl;
            exit(1);
        }
    }

    for (i = 0; i < num_threads; ++i) {
        cout<<"***** joining thread number: "<< i+1 <<endl;
        if (pthread_join(threadArray[i], NULL) != 0) {
            cout<<"Error joining thread"<<endl;
            exit(1);
        }
    }

    return 0;
}

peterpan@mailsrv0 ~$ g++ t.cc -D_REENTRANT -lpthread -Wall   
peterpan@mailsrv0 ~$ ./a.out 100 1000 #try some times
***** Creating thread number: 1
***** Creating thread number: 2
***** Creating thread number: 3
***** Creating thread number: 4
***** Creating thread number: 5
***** Creating thread number: 6
***** Creating thread number: 7
***** Creating thread number: 8
***** Creating thread number: 9
***** Creating thread number: 10
***** Creating thread number: 11
***** Creating thread number: 12
***** Creating thread number: 13
***** Creating thread number: 14
***** Creating thread number: 15
***** Creating thread number: 16
***** Creating thread number: 17
***** Creating thread number: 18
***** Creating thread number: 19
***** Creating thread number: 20
***** Creating thread number: 21
***** Creating thread number: 22
***** Creating thread number: 23
***** Creating thread number: 24
***** Creating thread number: 25
***** Creating thread number: 26
***** Creating thread number: 27
***** Creating thread number: 28
***** Creating thread number: 29
***** Creating thread number: 30
***** Creating thread number: 31
***** Creating thread number: 32
***** Creating thread number: 33
***** Creating thread number: 34
***** Creating thread number: 35
***** Creating thread number: 36
***** Creating thread number: 37
***** Creating thread number: 38
***** Creating thread number: 39
***** Creating thread number: 40
***** Creating thread number: 41
***** Creating thread number: 42
***** Creating thread number: 43
***** Creating thread number: 44
***** Creating thread number: 45
***** Creating thread number: 46
***** Creating thread number: 47
***** Creating thread number: 48
***** Creating thread number: 49
***** Creating thread number: 50
***** Creating thread number: 51
***** Creating thread number: 52
***** Creating thread number: 53
***** Creating thread number: 54
***** Creating thread number: 55
***** Creating thread number: 56
***** Creating thread number: 57
***** Creating thread number: 58
***** Creating thread number: 59
***** Creating thread number: 60
***** Creating thread number: 61
***** Creating thread number: 62
***** Creating thread number: 63
***** Creating thread number: 64
***** Creating thread number: 65
***** Creating thread number: 66
***** Creating thread number: 67
***** Creating thread number: 68
***** Creating thread number: 69
***** Creating thread number: 70
***** Creating thread number: 71
***** Creating thread number: 72
***** Creating thread number: 73
***** Creating thread number: 74
***** Creating thread number: 75
***** Creating thread number: 76
***** Creating thread number: 77
***** Creating thread number: 78
***** Creating thread number: 79
***** Creating thread number: 80
***** Creating thread number: 81
***** Creating thread number: 82
***** Creating thread number: 83
***** Creating thread number: 84
***** Creating thread number: 85
***** Creating thread number: 86
***** Creating thread number: 87
***** Creating thread number: 88
***** Creating thread number: 89
***** Creating thread number: 90
***** Creating thread number: 91
***** Creating thread number: 92
***** Creating thread number: 93
***** Creating thread number: 94
***** Creating thread number: 95
***** Creating thread number: 96
***** Creating thread number: 97
***** Creating thread number: 98
***** Creating thread number: 99
***** Creating thread number: 100
***** joining thread number: 1
***** joining thread number: 2
***** joining thread number: 3
***** joining thread number: 4
***** joining thread number: 5
¶Î´íÎó (core dumped)
~~~~~'Segmentation fault' in chinese
 



More information about the Gcc-bugs mailing list