Subj : Re: killing thread To : comp.programming.threads From : David Butenhof Date : Wed Aug 10 2005 03:08 pm Vitale Ferruccio wrote: > Hi all, > > I've two threads: the former blocked in an sleep(seconds) and the latter > which have to kill the former. Even if I call pthread_cancel or deliver > a SIGTERM signal with pthread_kill, the former thread continues to > run and it exits only after timer expires. > > Any ideas? What OS? If Linux, which version and thread implementation (specifically, Linuxthreads or NPTL)? What do you mean by "continues to run"? It was sleeping, right, not running? So do you really mean that it "continues to sleep", or do you mean that it wakes from the sleep but fails to TERMINATE as you expected? Note that if you don't have a signal action for SIGTERM, "sending SIGTERM", even with pthread_kill(), will terminate the PROCESS, not a thread. The process control signals are an integral part of POSIX/UNIX job control, and changing the semantics from process level to thread level would have broken a lot of things. Now, if you do have a signal action for SIGTERM, fine -- though SIGTERM is an odd choice. (Better to reserve that for process termination as intended, and use something else like SIGUSR1 or a realtime signal to interrupt a thread... or even better, avoid signals entirely.) Delivery of the signal, presuming the signal isn't blocked by the target, will interrupt any interruptable call. In particular, sleep() ought to return to the caller after interruption, with the remaining sleep time. (Could the sleep() call be in a loop? Otherwise, what would the thread do when sleep returned prematurely?) Similarly, if cancellation is enabled for the sleeping thread, pthread_cancel() will interrupt the sleep and cancel the thread. But do you know that cancel is enabled? Are there POSIX cleanup handlers (or C++ destructors or catch(...) blocks) in scope... and if so, are you sure that they'll complete and allow the thread to terminate? -- Dave Butenhof, David.Butenhof@hp.com HP Utility Pricing software, POSIX thread consultant Manageability Solutions Lab (MSL), Hewlett-Packard Company 110 Spit Brook Road, ZK2/3-Q18, Nashua, NH 03062 .