Simple example of threading

Gordon McMillan gmcm at hypernet.com
Thu Feb 24 11:22:33 EST 2000


Dirk Vleugels wrote:

> ... Under posix_threads there is pthread_cancel. This call
> is used to 'cancel' misbehaving threads (blocking on certain system
> calls (see doc)). The thread may ignore cancel requests, but this is
> under user control. 
> 
> Sadly, pthread_cancel is missing in python, dunno why.

For the same reason that Java has deprecated it. In almost all 
implementations, you can't interrupt a blocking call; all you 
can do is queue up a "cancel" that the blocked thread will get 
after (if ever) it returns from the blocking call. That's easy to do 
at the application level.

"Killing" a thread is very dangerous. Most OSes implement 
threads as "lightweight processes". The fat that has been 
trimmed is all the automatic clean-up. So resources (in 
particular, semaphores and other locks) that the thread has a 
claim on will not be released, leaving the process in an 
indeterminate state.

Java has also added timeouts to socket calls to help deal with 
this. It's a safe bet that that's actually select with a timeout, 
followed by the socket call. So maybe in a couple years, Java 
will let you multiplex multiple sockets in one thread, as God 
intended <wink>.

- Gordon




More information about the Python-list mailing list