killing a thread

Derek Thomson derek at
Thu Aug 5 02:52:25 CEST 1999

pascucci wrote:
> Is there a way to kill a thread from the process that generated it?
> I would like to do it both on Windows and Unix (sgi irix) operating
> systems.
> I know that this should be avoided in general but I have
> a computation loop that I wrote in C to optimize speed and for
> performance
> reasons I do not want to put in the inner loop checks for conditions
> that will make the
> thread exit gracefully.
> Valeiro

Then don't put the checks in the inner loop! Or check every n iterations
of the inner loop. Or just let the computation end, and then exit (will
it really waste that many CPU cycles? - you'll have to decide as it
depends on what you're doing).

I couldn't find anything in the thread or threading lib doco to kill a
thread externally, but I presume you looked there already.

In general, I want to warn you away from unilaterally killing a thread
from another thread. It's a bad idea. How do you intend to free any
resources acquired by the thread? Even if you are certain that you've
handled this *today*, tomorrow someone will modify the thread code and
not realize the implications of the fact that the thread could die *at
any time*.

I've never written code that kills a thread directly from another thread
without eventually unpicking it and replacing it with a mechanism that
allows the thread to realize it's time is up, and gracefully shut itself

Derek Thomson                               derek at
DSTC Pty Ltd                     
University of Qld, 4072                tel +61 7 3365 4310
AUSTRALIA                              fax +61 7 3365 4311

More information about the Python-list mailing list