Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it necessary to call pthread_join()

I create more than 100 threads from my main() so I just wanted to know that do I need to call pthread_join() before I exit my main(). Also, I do not need the data generated by these threads, basically, all the threads are doing some job independent from main() and other threads.

like image 988
nav_jan Avatar asked May 15 '12 12:05

nav_jan


People also ask

What is the need of having pthread_join () function?

The pthread_join() function provides a simple mechanism allowing an application to wait for a thread to terminate. After the thread terminates, the application may then choose to clean up resources that were used by the thread.

What happens without pthread_join?

with no pthread_join() it won't print anything! You can create threads without calling pthread_join simply by creating threads and not calling pthread_join.

Do I need to call pthread_exit?

You don't have to call pthread_exit() . Returning from the thread function would work equally well, and will not leak any resources (of course, you still have to ensure that your code doesn't have any leaks).

Can any thread call pthread_join?

Yes, this is possible. Indeed, this possibility is one of the main reasons why pthread_detach() exists.


4 Answers

pthread_join does two things:

  1. Wait for the thread to finish.
  2. Clean up any resources associated with the thread.

If you exit the process without joining, then (2) will be done for you by the OS (although it won't do thread cancellation cleanup, just nuke the thread from orbit), and (1) will not. So whether you need to call pthread_join depends whether you need (1) to happen.

If you don't need the thread to run, then as everyone else is saying you may as well detach it. A detached thread cannot be joined (so you can't wait on its completion), but its resources are freed automatically if it does complete.

like image 197
Steve Jessop Avatar answered Oct 17 '22 21:10

Steve Jessop


Yes if thread is attachable then pthread_join is must otherwise it creates a Zombie thread.

Agree with answers above, just sharing a note from man page of pthread_join.

NOTES

   After a successful call to pthread_join(), the caller is guaranteed that the target thread has terminated.

   Joining with a thread that has previously been joined results in undefined behavior.

   Failure to join with a thread that is joinable (i.e., one that is not detached), produces a "zombie thread".  Avoid doing this, since each zombie thread consumes some  system  resources,  and  when
   enough zombie threads have accumulated, it will no longer be possible to create new threads (or processes).
like image 30
Shahid Hussain Avatar answered Oct 17 '22 21:10

Shahid Hussain


When you exit, you do not need to join because all other threads and resources will be automatically cleaned up. This assumes that you actually want all the threads to be killed when main exits.

If you don't need to join with a thread, you can create it as a "detached" thread by using pthread_attr_setdetachstate on the attributes before creating the thread. Detached threads cannot be joined, but they don't need to be joined either.

So,

  1. If you want all threads to complete before the program finishes, joining from the main thread makes this work.

  2. As an alternative, you can create the threads as detached, and return from main after all threads exit, coordinating using a semaphore or mutex+condition variable.

  3. If you don't need all threads to complete, simply return from main. All other threads will be destroyed. You may also create the threads as detached threads, which may reduce resource consumption.

like image 3
Dietrich Epp Avatar answered Oct 17 '22 23:10

Dietrich Epp


By default threads in pthreads library are created as joinable.

Threads may, however, detach, rendering them no longer joinable. Because threads consume system resources until joined, just as processes consume resources until their parent calls wait(), threads that you do not intend to join must be detached, which is a good programming practice.

Of course once the main routine exits, all threading resources are freed.

If we fail to do that(detaching), then, when the thread terminates it produces the thread equivalent of a zombie process. Aside from wasting system resources, if enough thread zombies accumulate, we won't be able to create additional threads.

like image 2
Gowtham Munukutla Avatar answered Oct 17 '22 22:10

Gowtham Munukutla