【发布时间】:2018-10-27 18:24:18
【问题描述】:
你好,有没有办法运行一组线程(不阻塞它们)并在主线程发出信号时停止?
例如在这个线程回调中:
void *threadCallback ( void * threadID) {
syncPrint("Thread %lu started . Waiting for stop signal\n", threadID);
pthread_mutex_lock(&stopSignalGuard);
int i = 0;
while(!stopSignal) {
i++;
syncPrint("increment : %d \n",i);
pthread_cond_wait(&stopCondition,&stopSignalGuard);
}
syncPrint("Stop signal received. Thread %lu will terminate...\n",(long)threadID);
pthread_mutex_unlock(&stopSignalGuard);
pthread_exit(NULL);
}
据我所知,while 循环没有有效运行。执行被 pthread_cond_wait(...) 阻塞。是否可以运行此循环,直到主线程向工作人员发出停止信号?或者是另一种方式来做到这一点?
谢谢!
【问题讨论】:
标签: multithreading pthreads threadpool