How does a thread knows when to exit?
SITUATION:
-while the main program must wait for the threads to run to completion.
-This can be done by using a prototype function called pthread_join.
-after all, a call to this function waits for the termination of the thread whose id is given by thread itself.
After you have called
pthread_join(ptherad_t &var)the main will wait untill all the threads for which you have called join have exited.once all the threads are completed their tasks,
when it calls
pthread_exit(NULL)main will exit.inside the thread after its task is completed you need to call
pthread_exit(NULL)which will stop the excution of the thread.But this is not mandatory and the thread can simply return which means that the thread has completed.when it(thread) calls
pthread_exit(NULL),the calling thread will exit.