Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pthread create and join problems

Status
Not open for further replies.

rotis23

Programmer
Aug 29, 2002
121
GB
i have a daemon process that creates and joins multiple threads.

every so often the pthread error check will fail and the program terminate.

under what conditions would the pthread_create function fail?

i'm really looking for some practical reasons. i believe that the code is correct.

i'm also having problems with pthread_join hanging randomly.

any ideas?

TIA, rotis23

Code:
*************************************************

        pthread_exit(NULL);     //end of thread_function

*************************************************

        int res;
        int lots_of_threads;
        int num_sites;
        pthread_t a_thread[5];
        void *thread_result;

        //initialise mutex - used to synchronise linked list between threads
        res = pthread_mutex_init(&queue_mutex, NULL);
        if (res != 0){printf("mutex creation failure.\n");

        //add multiple threads
        for(lots_of_threads = 0; lots_of_threads < 5; lots_of_threads++)
        {
                //threads are created and started
                res = pthread_create(&(a_thread[lots_of_threads]), NULL,
thread_function, (void *)lots_of_threads);
                if (res != 0){printf(&quot;create failure.\n&quot;);
        }

        //wait for threads to finish and clean up
        printf(&quot;Waiting for threads to finish...\n&quot;);

        for(lots_of_threads = 5 - 1; lots_of_threads >= 0;
lots_of_threads--)
        {
                res = pthread_join(a_thread[lots_of_threads], NULL);
                if (res == 0)
                {
                        printf(&quot;Picked up a thread\n&quot;);
                }
                else{printf(&quot;join error.\n&quot;);
        }
 
There is normally some limit on how many threads you can create. If you hit that limit, then pthread_create will not be able to create anymore threads. Instead on creating and joining, you could go for the thread pool idea. This is described in the Nutshell book on pthreads.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top