gpt4 book ai didi

c - 如何停止在 pthread_join 上停滞的线程?

转载 作者:太空宇宙 更新时间:2023-11-04 08:00:01 25 4
gpt4 key购买 nike

我有一个项目,我要将作业添加到队列中,并且有多个线程执行作业,并计算它们自己的独立结果。

我的程序处理 SIGINT 信号,我正尝试加入线程以将结果相加,打印到屏幕,然后退出。我的问题是当我发送信号时线程似乎停止运行,或者它们在 mutex_lock 上被阻塞。以下是我的程序的重要部分,以求简洁。

ma​​in.c

//the thread pool has a queue of jobs inside
//called jobs (which is a struct)
struct thread_pool * pool;

void signal_handler(int signo) {
pool->jobs->running = 0; //stop the thread pool
pthread_cond_broadcast(pool->jobs->cond);

for (i = 0; i < tpool->thread_count; i++) {
pthread_join(tpool->threads[i], retval);
//do stuff with retval
}

//print results then exit
exit(EXIT_SUCCESS);
}

int main() {
signal(SIGINT, signal_handler);
//set up threadpool and jobpool
//start threads (they all run the workerThread function)
while (1) {
//send jobs to the job pool
}
return 0;
}

thread_stuff.c

void add_job(struct jobs * j) {
if (j->running) {
pthread_mutex_lock(j->mutex);
//add job to queue and update count and empty
pthread_cond_signal(j->cond);
pthread_mutex_unlock(j->mutex);
}
}

struct job * get_job(struct jobs * j) {

pthread_mutex_lock(j->mutex);

while (j->running && j->empty)
pthread_cond_wait(j->cond, j->mutex);

if (!j->running || j->empty) return NULL;

//get the next job from the queue
//unlock mutex and send a signal to other threads
//waiting on the condition
pthread_cond_signal(j->cond);
pthread_mutex_unlock(j->mutex);
//return new job
}

void * workerThread(void * arg) {
struct jobs * j = (struct jobs *) arg;
int results = 0;
while (j->running) {
//get next job and process results
}
return results;
}

谢谢你的帮助,这让我很头疼!

最佳答案

您不应从处理异步生成的信号(如 SIGINT)的信号处理程序调用 pthread_cond_waitpthread_join。相反,您应该为所有线程阻止 SIGINT,生成一个专用线程,并在那里调用 sigwait。这意味着您在信号处理程序上下文之外检测到 SIGINT 信号的到达,因此您不限于 async-signal-safe functions .如果信号被传送到其中一个工作线程,您还可以避免自死锁的风险。

此时,您只需有序地关闭您的工作队列/线程池即可。根据详细信息,您现有的带有 running 标志的方法甚至可能不会发生变化。

关于c - 如何停止在 pthread_join 上停滞的线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47365980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com