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
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.