nav_jan
nav_jan

Reputation: 2553

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.

Upvotes: 24

Views: 40074

Answers (7)

Shahid Hussain
Shahid Hussain

Reputation: 1789

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

I agree with the 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).

Upvotes: 6

Gowtham Munukutla
Gowtham Munukutla

Reputation: 45

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.

Upvotes: 2

alk
alk

Reputation: 70911

Per default a thread runs attached, that means the resources it needs are kept in use until the thread is joined.

As from your description noone but the thread itself needs the thread's resources, so you might create the thread detached or detach the thread prior to having it started.

To detach a thread after its creation call pthread_detach().

Anyhow if you want to make sure all threads are gone before the program ends, you should run the threads attached and join them before leaving the main thread (the program).

Upvotes: 1

Steve Jessop
Steve Jessop

Reputation: 279245

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.

Upvotes: 39

Dietrich Epp
Dietrich Epp

Reputation: 213318

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.

Upvotes: 3

Didier Trosset
Didier Trosset

Reputation: 37437

If you want to be sure that your thread have actually finished, you want to call pthread_join.

If you don't, then terminating your program will terminate all the unfinished thread abruptly.

That said, your main can wait a sufficiently long time until it exits. But then, how can you be sure that it is suffucient?

Upvotes: 0

fbernardo
fbernardo

Reputation: 10104

If your main ends your application ends and your threads die... So you do need to use thread join (or use fork instead).

Upvotes: -1

Related Questions