views:

95

answers:

2

What are the benefits of thread joining ? If the point is to join a thread to stop a thread A until thread B is finished executing, for instance (B.join()) why not use a global variable to do this?

+5  A: 

Because join() allows A to go to sleep and not consume CPU time until B ends, and it also ensures that B has completely ended before continuing with A. In general it's just far more efficient. It also avoids the hassle of creating an additional variable.

Amber
+1  A: 

Another reason you want to call pthread_join() or pthread_detach() to thread that was created with flag PTHREAD_CREATE_JOINABLE is: the system can reclaim all resources associated with the thread after thread termination. Failure to join to or detach joinable threads causes memory and other resource leaks until the process ends.

pierr