Thread: Variable threads running on basic pthreaded code...not sure why

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    Variable threads running on basic pthreaded code...not sure why

    I've got a simple pthread example. When I run the executable I get a different number of threads running each time when I assumed I should be always generating and running 5 threads.

    My compile statement:
    Code:
    $ gcc -Wall ptdetached.c -o runmain -pthread
    Here is a sample of the last few runs:
    Code:
    $ ./runmain 
    Thread 296150784 started
    Thread 296150784 has detached and will end on it's own
    Thread 279365376 started
    Thread 279365376 has detached and will end on it's own
    Thread 287758080 started
    Thread 287758080 has detached and will end on it's own
    Thread 262579968 started
    Thread 262579968 has detached and will end on it's own
    Thread 270972672 started
    Thread 270972672 has detached and will end on it's own
    $ ./runmain 
    Thread -100919552 started
    Thread -100919552 has detached and will end on it's own
    $ ./runmain 
    Thread 1415571200 started
    Thread 1415571200 has detached and will end on it's own
    $ ./runmain 
    Thread 469026560 started
    Thread 469026560 has detached and will end on it's own
    $ ./runmain 
    Thread -1016367360 started
    Thread -1016367360 has detached and will end on it's own
    Code:
    #include <pthread.h>
    #include <stdio.h>
    
    //GLOBALS
    #define MAX_THREADS 5
    
    void *threadWorker( void *arg )
    {
        //LOCALS
        int ret;
    
        printf( "Thread %d started\n" , (int)pthread_self() );
        
        ret = pthread_detach( pthread_self() );
        if ( ret != 0 ){
            printf( "error detaching pthread %d\n", (int)pthread_self() );
        }   
        else{  printf( "Thread %d has detached and will end on it's own\n" , (int)pthread_self() ); }
       
        pthread_exit( arg );
    }
    
    int main()
    {
        //LOCALS
        int ret, i, status;
        pthread_t threadIds[MAX_THREADS];
    
        for (i=0; i < MAX_THREADS ; i++)
        {   
            ret = pthread_create( &threadIds[i], NULL, threadWorker, (void *)i );  
            if (ret != 0)
            {   
                printf( "error creating thread %d\n" , (int)threadIds[i] );
            }   
         
        }   
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    With pthreads, the thread that spawned the others does not automatically wait for "child" threads to finish before it exits. Couple that with the fact that the pthreads library does not guarantee any particular thread execution order, and you return to your "parent" thread after an arbitrary number of child threads have executed. That parent thread makes no attempt to wait for all it's children to finish, and just exits, killing off all your threads, sometimes before all of them have had a chance to finish.

    It's a little bit analogous to what would happen if you forked several processes to do a large load of work, and then immediately exited from the parent. You would have a bunch of orphaned children that init would have to come along and reap. Except with threads, there is no init to reap them and keep them alive until they finish. They simply die off.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If main() finishes before the threads it has created, and exits with pthread_exit(), the other threads will continue to execute. Otherwise, they will be automatically terminated when main() finishes.
    https://computing.llnl.gov/tutorials/pthreads/
    Exit main function by calling pthread_exit()

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Here's a good tutorial and reference for the pthreads library: https://computing.llnl.gov/tutorials/pthreads/.

    Also, if you want to guarantee that all your threads finish before exiting, put a pthread_exit(NULL); at the end of your main function.

    EDIT: Beaten by Bayint!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. How make static variable for across threads?
    By 6tr6tr in forum C++ Programming
    Replies: 10
    Last Post: 04-22-2008, 08:32 AM
  3. Replies: 1
    Last Post: 12-18-2007, 02:51 PM
  4. Pthreaded server
    By tezcatlipooca in forum Linux Programming
    Replies: 6
    Last Post: 11-29-2007, 01:10 PM
  5. Replies: 2
    Last Post: 02-28-2002, 03:27 PM