Problems with PyGILState_Ensure () and PyGILState_Release ()

Mathias Mamsch Domoran at
Mon Aug 16 19:00:08 CEST 2004

Hi Greg, thanks for your answer ...

I really need help with this, because till now I found no solution to my
problem ... I always get FatalErrors, Deadslocks, or protection faults ...

I created some example code for what I want to accomplish. I have a single
thread which calls some C-Function which should call back into python.

To your questions: no nowhere in *my* code ReleaseThread is called and I
dont write to gstate either ...
I dont understand the thread handling of python completely... Any ideas
where - besides the Python docs - I can find some information about that  ?

Thanks Mathias Mamsch

here is some example code which produces my problem.
hope it helps to understand it ...


#include "python.h"
#include <windows.h>

#include <iostream>

using namespace std;

#define GETLOCK     PyGILState_STATE gstate; \
                    gstate = PyGILState_Ensure ();

#define RELEASELOCK PyGILState_Release (gstate);

static DWORD Tid;

static int CHandleFunction ()
    // here will I callback to python ...
    cout << "Hello" << endl;

    return 0;

static PyMethodDef SpamMethods[] = {
    {NULL, NULL, 0, NULL}        /* Sentinel */

// some C Thread which calls the Handler ...
DWORD __stdcall WCThread (LPVOID lpThreadParameter)
        while (1)

PyMODINIT_FUNC initkdll(void)
    Py_InitModule("kdll", SpamMethods);
    // Create some C++ Thread for testing purposes

More information about the Python-list mailing list