【发布时间】:2021-01-26 03:20:50
【问题描述】:
void * worker(void * arg){
threadData * p = (threadData *)arg; // Has thread info like what spots in the array this thread should change
rowHead * row_head = p->info; // Has what rows need to mutiplied and which ones subtracted from
pthread_cond_wait(&no_task_cond, &no_task_mutex); // Wait for new data to be added.
pthread_mutex_unlock(&no_task_mutex); // Instantly give up lock since there is no critical data to protect
pthread_cond_broadcast(&no_task_cond);
// Go through the tasks and complete them
rowNode * row_info = row_head->head;
while(row_info != NULL){ // If we have a task then execute
thread_multiplication(p->rank, p->t_row, row_info->row, row_info->multi, row_info->sub_row, p->dim, *p->matrix);
row_info = row_info->next;
}
return 0;
}
我希望一组线程进入工作线程。等待所有任务都加入到链表 row_info 中,一旦主线程调用 p_thread_broadcast(&no_task_mutex) 唤醒所有线程,在退出函数之前遍历链表并执行它们的任务。
我不确定,但我很确定它在一开始就卡在等待条件上,这对我来说毫无意义,因为它应该总是至少调用一次 p_thread_broadcast。这可能与我的等待和广播有关,因为它们似乎并没有像我认为的那样工作。当我有一个 cond_wait 并调用一个应该唤醒所有线程的广播时,如果它们立即放弃锁,它们应该同时进行,对吗?特别是如果我之后添加了广播以确保安全
【问题讨论】: