Subj : Re: atexit handler: pthread_cancel, pthread_testcancel, pthread_join problem To : comp.programming.threads From : loic-dev Date : Fri Aug 19 2005 01:56 am Halla Anna, > > I share Joe's opinion here. It doesn't make a lot of sense to register > > an atexit handler that cancels the thread. The function exit() does > > just that. > > Does this suggest that exit() will do a pthread_cancel() for > other threads running? For example, if I have a thread whose > start routine is Usually, no. When you call exit() all the threads within the process cease to exist. A thread doesn't necessarily terminate at a cancellation point. I guess, the question you should ask yourself is: Is there a reason why I want to cancel my thread upon process exit? Can my thread just be terminated anywhere? > void* start_thread(void* something) > { > for(;;) > { > // lengthy operation A > pthread_testcancel(); // op. B > // lengthy op. C > } > } > > If I don't call pthread_cancel() explicitly for the above thread, > will the above thread still get cancelled only at operation B? "Cancelled" is the wrong word. It might be terminated during operation A, during the pthread_testcancel() or during operation C. If - for some reasons - you really need that your process exits only at cancellation points, proceed (for instance) as follows: 1) dedicate a thread that waits on the condition variable "process is exiting". 2) after wake up, this thread cancels the other threads. 3) Finally it eventually joins, and call exit(). 4) At the places where the process must terminate, signal the condition variable "process is existing" HTH, Loic. .