[capi-sig] Thread structures BUG?

Pablo Yabo pablo.yabo at gmail.com
Sat Nov 3 22:58:06 CET 2007

I create lots of threads that run a script and I have a problem when a
thread is created with the same threadId that existed before.

When a new thread is started I use this code to enter the interpreter:
    // create a thread state object for this thread
threadState = PyThreadState_New(mainInterpreterState);

and when the thread finishes:

// delete my thread state object

Everything works smoothly until a thread id is re-used.
I've been testing what is happening and the problem is that
PyGILState_GetThisThreadState() returns a PyThreadState for the reused
thread id.
I changed my code to call PyGILState_GetThisThreadState() before creating
the PyThreadState. But if I try to use the returned PyThreadState (that
shouldn't exist because I've already deleted) I got an error when trying to
access the interpreter dictionary.

The workaround that I found is to keep all PyThreadState structures without
calling PyThreadState_Clear / PyThreadState_Delete. When a new thread is
created I call PyGILState_GetThisThreadState(). If it returns something I
reuse the structure.

In this way, my code works.

Pablo Yabo


More information about the capi-sig mailing list