Threading and GIL

Carl Banks pavlovevidence at gmail.com
Thu May 7 02:30:48 CEST 2009


On May 6, 5:13 pm, googler.1.webmas... at spamgourmet.com wrote:
> Hi!
>
> I have a big problem I can't solve and I hope anyone of you can help
> me. I use the Python C API and in C++ I have a class which represets a
> thread object, similiar to the thread class of the known Python Thread
> class but with some needed additions so thats the reason why I have to
> built my own. Well, this is the problem.
>
> There is a method called Thread::End() which I can call in Python. But
> now the End() Function waits until the "Main" Function of my C++ class
> is done, but in this method I want to ensure the Global Interpreter
> Lock but this is already locked by the Thread which executes the End()
> command. So both is waiting and stops. The thread which calls the End
> () method is waiting for the finish of the Main() method of the Thread
> Class and the Main-Method in the other thread waits for the thread of
> the End() command that this is done - welcome Deadlock.
>
> Any suggestions what I have to do? Thank you veeery much for you help,
> otherwise I become really crazy here because this is the first time I
> have such a problem.
>
> Bye :)

You can try the PyGILState_Ensure and PyGILState_Release macros (see
PEP 311, http://www.python.org/dev/peps/pep-0311/ for more
information).

Make sure you have the GIL whenever you make any calls into Python
(with a couple exceptions) and make sure you DON'T have the GIL when
creating or deleting threads.  In fact, I can't think of any reason
why it wouldn't work to ensure a GIL state first thing after the
thread starts and to release it right before the thread ends.

Carl Banks



More information about the Python-list mailing list