我想在我的主線程的另一個線程中打破sleep (3)
。要做到這一點,我想在使用raise (sig)
並在打破系統調用後繼續執行代碼。如何喚醒睡眠(3)ing線程
那麼,我應該使用哪個信號來導致sleep
並返回EINTR
?
我寫到目前爲止代碼:
void *event_loop (void *threadid)
{
int rc;
long tid;
do{
rc = sleep(20);
printf ("return from sleep with %d", rc);
fprintf(stderr, "Value of errno: %d\n", errno);
} while (errno == EINTR);
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld! = %d\n", tid);
}
void main(int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
int sig=SIGABRT ; // which signal???
int i=0;
printf("Main THREAD\n");
for(t=0;t<2;t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, event_loop, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
sleep(2);
printf("rasing signal now from main thread...\n");
raise(sig);
pthread_exit(NULL);
}
經典[XY問題](http://xyproblem.info)。你真的想做什麼? –
我附加的程序應該解釋一下,我嘗試通過raise() – bzzzz7
生成EINTR我重新解釋了你的問題,所以它表達了你想解決的實際問題。 – a3f