threads killing other threads

Jeff Shannon jeff at ccvcorp.com
Thu Feb 7 15:17:26 EST 2002


Greg Weeks wrote:

> I've occasionally wanted thread objects to have an exit() method, so that
> one thread could interrrupt and kill another.  This doesn't seem to be
> available, perhaps because POSIX threads don't support it.
>
> Is that correct?
>
> Is there some reason why threads killing threads would be undesirable or
> hard to implement?

I don't know the reasoning behind threads being unkillable (from outside),
but I do know the (apparently standard) way to accomplish what you want.

Create a global threading.Event object, named something descriptive like
Quit, and have every thread peek at it occasionally.  If the Quit event is
set, then the thread aborts.  If you need to be able to kill specific threads
individually, then you have a separate event object for each thread.

Of course, this *does* rely on the threads being well-behaved, and not
getting stuck in an infinite loop elsewhere, and so on....  but then, that's
pretty much a requirement anyhow.

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list