Thread: mysterious segmentation fault

  1. #1
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92

    mysterious segmentation fault

    Hi,

    Here is the code:
    Code:
    #include <pthread.h>
    #include <iostream>
    #include <vector>
    using namespace std;
    
    vector<string> myVector[3];
    vector<string>::iterator myVectorIterator;
    
    void* function( void* parameters )
    {
        int* i = (int *)parameters;
        bool keep = true;
    
        cout << "This thread prints number " << *i << endl;
        while(keep)
        {
    	for( myVectorIterator = myVector[*i].begin();
    		myVectorIterator != myVector[*i].end();
    		myVectorIterator++ )
    	{
    	    cout << "String thread : " << *myVectorIterator << endl;
    	    sleep(1);
    	}
        }
    
        return NULL;
    }
    
    int main()
    {
        pthread_t thread_id[3];
    
        //fill the vector
        myVector[0].push_back("Stupid dam_n you");
        myVector[0].push_back("I hate you!");
        myVector[0].push_back("Could you die, please!");
        myVector[1].push_back("I love you!");
        myVector[1].push_back("Beautiful life...");
        myVector[1].push_back("Sea coming from heaven...");
        myVector[2].push_back("Peace mind...");
        myVector[2].push_back("Glory of the heart!");
        myVector[2].push_back("Kindness means happiness...");
    
        for(int i=0; i<3; i++)
        {
    	pthread_create( &thread_id[i], NULL, &function, &i );
        }
    
        for(int i=0; i<3; i++)
        {
    	pthread_join(thread_id[i], NULL);
        }
    
        return 0;
    }
    I always get this output:
    This thread prints number 0
    String thread : Stupid dam_n you
    This thread prints number 1
    String thread : I love you!
    This thread prints number 2
    String thread : Peace mind...
    String thread : Glory of the heart!
    String thread : Kindness means happiness...
    Segmentation fault

    Why???? If I remove the sleep function, it is okay... but just for a while, it will segmentation fault at some point. The interesting part is when I remove the sleep function, the program will display string thread from first vector for couple seconds then when it started to display string thread from second vector, it get segmentation fault.
    The output is like this:
    String thread : Stupid dam_n you
    String thread : I hate you!
    String thread : Could you die, please!
    String thread : Stupid dam_n you
    String thread : I hate you!
    String thread : Could you die, please!
    String thread : Stupid dam_n you
    String thread : I hate you!
    String thread : Could you die, please!
    String thread : Stupid dam_n you
    String thread : I hate you!
    String thread : Could you die, please!
    String thread : Stupid dam_n you
    String thread : I hate you!
    String thread : Could you die, please!
    String thread : Stupid dam_n you
    String thread : I hate you!
    String thread : Could you die, please!
    you die, please!
    String thread : I love you!
    String thread : Beautiful life...
    String thread : Sea coming from heaven...
    Segmentation fault

    What I want from my program is the program display the string thread fairly and no segmentation fault. How? Thank you.
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > for(int i=0; i<3; i++)
    As soon as this loop exists, all your threads are then pointing at a variable which no longer exists.
    So when they dereference their pointer, the value they get (if they get anything at all) is anyones guess.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Quote Originally Posted by Salem
    > for(int i=0; i<3; i++)
    As soon as this loop exists, all your threads are then pointing at a variable which no longer exists.
    So when they dereference their pointer, the value they get (if they get anything at all) is anyones guess.
    Ok, I change the code to:
    Code:
    #include <pthread.h>
    #include <iostream>
    #include <vector>
    using namespace std;
    
    vector<string> myVector[3];
    vector<string>::iterator myVectorIterator;
    
    void* function( void* parameters )
    {
        int* i = (int *)parameters;
        bool keep = true;
    
        cout << "This thread prints number " << *i << endl;
        while(keep)
        {
    	for( myVectorIterator = myVector[*i].begin();
    		myVectorIterator != myVector[*i].end();
    		myVectorIterator++ )
    	{
    	    cout << "String thread : " << *myVectorIterator << endl;
    	    sleep(1);
    	}
        }
    
        return NULL;
    }
    
    int main()
    {
        pthread_t thread_id[3];
    
        //fill the vector
        myVector[0].push_back("Stupid damn you");
        myVector[0].push_back("I hate you!");
        myVector[0].push_back("Could you die, please!");
        myVector[1].push_back("I love you!");
        myVector[1].push_back("Beautiful life...");
        myVector[1].push_back("Sea coming from heaven...");
        myVector[2].push_back("Peace mind...");
        myVector[2].push_back("Glory of the heart!");
        myVector[2].push_back("Kindness means happiness...");
    
        int a = 0;
        int b = 1;
        int c = 2;
        //for( i=0; i<3; i++)
        //{
    	pthread_create( &thread_id[a], NULL, &function, &a );
    	pthread_create( &thread_id[b], NULL, &function, &b );
    	pthread_create( &thread_id[c], NULL, &function, &c );
        //}
    
        for(int j=0; j<3; j++)
        {
    	pthread_join(thread_id[j], NULL);
        }
    
        return 0;
    }
    The output is not fair at all:
    This thread prints number 0
    String thread : Stupid damn you
    This thread prints number 1
    String thread : I love you!
    This thread prints number 2
    String thread : Peace mind...
    String thread : Glory of the heart!
    String thread : Kindness means happiness...
    String thread : Peace mind...
    String thread : Glory of the heart!
    String thread : Kindness means happiness...
    String thread : Peace mind...
    String thread : Glory of the heart!
    String thread : Kindness means happiness...
    String thread : Peace mind...
    String thread : Glory of the heart!
    String thread : Kindness means happiness...
    String thread : Peace mind...
    String thread : Glory of the heart!
    String thread : Kindness means happiness...
    String thread : Peace mind...
    String thread : Glory of the heart!
    String thread : Kindness means happiness...
    String thread : Peace mind...
    String thread : Glory of the heart!
    String thread : Kindness means happiness...
    String thread : Peace mind...

    How can I make the program to be more fair??? My program forgot that he has thread number one and two. My program prints thread number three only after it has printed thread number one and two once.
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well sleep() is a process-level function, not a thread-level function. Calling sleep stops everything in the current process (as far as I know).

    pthread_cond_timedwait() seems to be the thing to use to cause a particular thread to wait for something to happen. If you choose a condition which is always false, then the return will depend solely on the timeout.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    I learned pthread_cond_timedwait from here:
    http://users.actcom.co.il/~choo/lupg...d_condvar_wait

    Here is the "make me confuse" code:
    Code:
    while (!done) {
        rc = pthread_cond_timedwait(&got_request, &request_mutex, &timeout);
        switch(rc) {
            case 0:  
                     
                done = 0;
                break;
            default:       
                if (errno == ETIMEDOUT) { 
                    done = 0;
                }
                break;   
        }
    }
    Look at the this line:
    if ( errno == ETIMEDOUT ) {

    Should it be:
    if ( rc == ETIMEDOUT ) {
    ?????

    I test that tutorial with my simple program:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <sys/time.h>
    #include <unistd.h>
    #include <errno.h>
    
    pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
    
    struct timeval now;
    struct timespec timeout;
    
    void *functionDummy()
    {
        pthread_mutex_lock( &mutex1 );
        int rc = pthread_cond_timedwait( &cond1, &mutex1, &timeout );
    
        printf( "errno is %d\n", errno );
    
        switch(rc)
        {
    	case 0:
    	    break;
    	default:
    	    if(errno == ETIMEDOUT)
    	    {
    		printf("Time out!\n");
    	    }
    	    break;
        }
    
        pthread_mutex_unlock( &mutex1 );
    }
    
    int main()
    {
        gettimeofday(&now, NULL);
    
        timeout.tv_sec = now.tv_sec + 2;
        timeout.tv_nsec = now.tv_usec * 1000;
    
        pthread_t thread1;
    
        pthread_create( &thread1, NULL, &functionDummy, NULL );
    
        pthread_join( thread1, NULL );
    
        return 0;
    }
    The output is
    errno is 0

    Thank you.
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM