到目前为止,我设置了一个全局变量taskCompleted
来跟踪已经完成了多少任务;executeTask()
在达到一定数量时终止。但是程序似乎在完成所有提交的任务后仍然挂起。这是为什么?
#define THREAD_NUM 4
typedef struct Task {
int a, b;
} Task;
Task taskQueue[256];
int taskCount = 0;
int taskCompeleted = 0;
pthread_mutex_t mutex;
pthread_cond_t taskAvailable;
void executeTask(Task* task) {
//usleep(50000);
int result = task->a + task->b;
printf("The sum of %d and %d is %d\n", task->a, task->b, result);
}
void submitTask(Task task) {
pthread_mutex_lock(&mutex);
taskQueue[taskCount] = task;
taskCount++;
pthread_cond_signal(&taskAvailable);
pthread_mutex_unlock(&mutex);
}
void* startThread(void* args) {
while (1) {
Task task;
pthread_mutex_lock(&mutex);
while (taskCount == 0) {
if (taskCompleted >= 10) {
pthread_mutex_unlock(&mutex);
return 0;
}
pthread_cond_wait(&taskAvailable, &mutex);
}
task = taskQueue[0];
int i;
for (i = 0; i < taskCount - 1; i++) {
taskQueue[i] = taskQueue[i + 1];
}
taskCount--;
taskCompeleted++;
pthread_mutex_unlock(&mutex);
executeTask(&task);
}
}
int main(int argc, char* argv[]) {
pthread_t th[THREAD_NUM];
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&taskAvailable, NULL);
int i;
for (i = 0; i < THREAD_NUM; i++) {
if (pthread_create(&th[i], NULL, &startThread, NULL) != 0) {
perror("Failed to create the thread");
}
}
for (i = 0; i < 10; i++) {
Task t = {
.a = i,
.b = i
};
submitTask(t);
}
for (int i = 0; i < THREAD_NUM; i++) {
if (pthread_join(th[i], NULL) != 0) {
perror("Failed to join the thread");
}
}
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&taskAvailable);
return 0;
}
1条答案
按热度按时间8e2ybdfx1#
我建议添加一个变量,线程可以在检查新任务是否可用的同时进行检查:
thread函数中的循环将变成(快速退出,即使还有任务剩余):
或者,仅在任务队列耗尽时退出:
然后可以调用
shutdown_queue
来通知所有线程终止。