Re: [capi-sig] capi-sig Digest, Vol 35, Issue 4

sorry about the attachment
On 06/05/2010 06:00 PM, capi-sig-request@python.org wrote:
Next problems, (and hope last)
I have started some Python threads, and want to wait for them to finish.
I wrote this program to demonstrate my problem. I am on a 64 bit machine, Ubuntu.
I tried playing around with GIL state but it does not change anything, so it is commented out.
when using pthreads it works (of course) when I use python threads the sleep does not work and the threads all exit on the _join
capi-sig mailing list capi-sig@python.org http://mail.python.org/mailman/listinfo/capi-sig
End of capi-sig Digest, Vol 35, Issue 4
#include <Python.h> #include <pthread.h> #include <time.h> #include <stdio.h>
typedef struct _thrdStruct { int num; pthread_t self; } thrdStruct;
void* thrdFunc( void *p ) { thrdStruct *thrd = ( thrdStruct* ) p; thrd->self = pthread_self(); //PyGILState_STATE gstate = PyGILState_Ensure();
printf( "THRD %d\n", thrd->num);
//Py_BEGIN_ALLOW_THREADS
sleep(15);
//Py_END_ALLOW_THREADS
//PyGILState_Release(gstate);
return NULL;
}
int main( int argc, char *argv[] ) { thrdStruct thrds[10]; pthread_t self[10] = {0}; int rv, i; void *stat; PyGILState_STATE gstate;
Py_Initialize();
PyEval_InitThreads();
gstate = PyGILState_Ensure();
for (i=0;i<10;i++) {
thrds[i].num = i;
rv = PyThread_start_new_thread( thrdFunc, &thrds[i] );
//rv = pthread_create( &self[i], NULL, thrdFunc,
&thrds[i] ); } PyGILState_Release(gstate);
//Py_BEGIN_ALLOW_THREADS
sleep(1);
//Py_END_ALLOW_THREADS
for (i=0;i<10;i++) {
if ( self[i] != thrds[i].self )
printf( "%d %x %x\n", i, self[i], thrds[i].self);
}
for (i=0;i<10;i++) {
rv = pthread_join( thrds[i].self, &stat );
}
Py_Finalize();
}
participants (1)
-
Shaun Savage