pthreads in C - pthread_exit

前端 未结 9 1383
北荒
北荒 2021-02-05 14:03

For some reason I thought that calling pthread_exit(NULL) at the end of a main function would guarantee that all running threads (at least created in the main funct

9条回答
  •  甜味超标
    2021-02-05 14:21

    pthread_exit() is a function called by a thread to terminate its own execution. For the situation you've given it is not to be called from your main program thread.

    As you have figured out, pthread_join() is the correct means to wait for the completion of a joinable thread from main().

    Also as you've figured out, you need to maintain the value returned from pthread_create() to pass to pthread_join().

    What this means is that you cannot use the same pthread_t variable for all the threads you create if you intend to use pthread_join().

    Rather, build an array of pthread_t so that you have a copy of each thread's ID.

提交回复
热议问题