在C中的循环中重用条件变量

问题描述 投票:1回答:1

是否可以在循环中重复使用条件变量?我试图创建一个线程池,每当线程轮到使用条件变量发出主线程信号时,就创建一个线程池。一开始,线程将等待信号,此后,它将完成其工作并保持循环,直到循环结束。我在下面尝试过此]

// one of the threads in the thread pool
while(a condition){
   pthread_cond_wait(&cond, &lock);       
   pthread_mutex_lock(&lock);
   // job
   pthread_mutex_unlock(&lock);
}

// in main thread, whenever a condition happens
// for a specific thread, main thread signals using condition variable
pthread_cond_signal(&cond);

此代码有什么问题?

是否可以在循环中重复使用条件变量?我试图创建一个线程池,每当线程轮到使用条件变量发出主线程信号时,就创建一个线程池。首先,...

c multithreading threadpool thread-synchronization
1个回答
1
投票

谢谢大家回答这个问题。我收集了您的答案和在网上找到的资源,并找到了针对自己问题的解决方案。顺序应该是这样的:

© www.soinside.com 2019 - 2024. All rights reserved.