Creating multiple python interpreter from C++ threads

Hi,
I am trying to create multiple python interpreter using Py_NewInterpreter() function in C++ thread. When i run the code sometime it works but sometime it just hanged .Looks like some threads are waiting to acquire the lock which has not been released and thats why a deadlock.
Can please anyone suggest whats wrong with the code????

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    void *threadFunc(void *arg)
    {
    PyThreadState* tState;
    PyEval_AcquireLock();
    tState = Py_NewInterpreter();
    PyThreadState_Swap(tState);
    PyRun_SimpleString("print \"hello\"");
        PyErr_Clear();
        Py_EndInterpreter(tState);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
    pthread_exit(NULL);
    }
    int main()
    {
     pthread_t p;
        int i;
     Py_InitializeEx(1);
        PyEval_InitThreads();
        mainThreadState = PyThreadState_Get();
    PyEval_ReleaseLock();
     for(i=0; i<10; i++)
        {
            pthread_create(&p, NULL, threadFunc, (void *)argv);
        }
      pthread_join(p, NULL);
        PyEval_RestoreThread(mainThreadState);
        Py_Finalize();
     pthread_exit(NULL);
        return 0;
    }
Topic archived. No new replies allowed.