thread, threading; how to kill a thread?

Thomas Heller theller at python.net
Thu Nov 18 02:38:58 EST 2004


David Bolen <db3l at fitlinxx.com> writes:

[...]
> Of course, while the above runs on my system, I do think it has a GIL
> risk - I expect that ctypes is releasing the GIL just before calling
> the native function, so there's a chance the other thread could resume
> and obtain the GIL just prior to TerminateThread executing at the
> Win32 layer.  My odds are probably lower in this example since the
> dummy thread is sleeping most of the time.  So if I was going to use
> this in practice I'd probably provide my own extension module that
> wrapped TerminateThread but made sure *not* to release the GIL before
> calling it.

For the record: Yes, ctypes releases and reacquires the GIL around
normal function calls.  But in recent releases this can even be
customized - there's a FUNCFLAG_PYTHONAPI flag which prevents this,
because it wouldn't be possible to call Python api functions otherwise.
See the sources for details (it also calls PyErr_Occurred() after the
function call, which is unneeded in your case but also doesn't hurt).

Thomas



More information about the Python-list mailing list