pthreads in C++ with embedded Python

Tom Brown nextstate at gmail.com
Tue Jun 7 19:29:49 EDT 2011


Hi guys!

I am trying to build a C++ application that uses pthreads and embedded
python. I've simplified the problem down so that the Python code is a single
class that subclasses from Queue. The main thread of the C++ application
adds to the queue. A worker thread in the C++ application reads from the
queue.

I am using:
PyGILState_Ensure()
PyGILState_Release()

to lock the GIL and release it.

I'm finding this situation happening:
1) main thread locks the GIL
2) worker thread locks the GIL
3) main thread tries to release the GIL, but the thread state is not current
and the program aborts with this error:
Fatal Python error: This thread state must be current when releasing

Other times, the program runs to completion, only to segfault just before it
exits.

C++ code can be found here:
http://pastie.org/2035072

Python code can be found here:
http://pastie.org/2035086


I've tried several different APIs without any success. This is my first
attempt at embedding Python. So, what is correct way to do this?

Any suggestions will be appreciated.

Thanks!
Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110607/633f2c97/attachment.html>


More information about the Python-list mailing list