c-停止一组线程,然后让它们全部离开


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;
}

我希望发生的事情是让一组线程进入worker。等待所有任务都添加到链表row_info中,然后一旦主线程调用p_thread_broadcast(&no_task_mutex(以唤醒所有线程,则在退出函数之前浏览链表并完成它们的任务。

我不确定,但我很确定它一开始就被困在等待条件下,这对我来说毫无意义,因为它应该至少调用p_thread_broadcast一次。这可能与我的等待和广播有关,因为它们似乎没有像我认为的那样工作。当我有一个cond_wait并调用一个广播时,该广播应该唤醒所有线程,如果它们立即放弃锁,它们应该同时运行,对吗?特别是如果我之后有广播,我添加它是安全的

pthread_cond_timedwait()pthread_cond_wait()函数应条件变量上的块应用程序应确保这些函数是在调用线程锁定互斥的情况下调用的;否则,错误(对于PTHREAD_MUTEX_ERRORCHECK和健壮互斥(或未定义的行为(对于其他互斥(结果

现在。。。在调用pthread_cond_wait之前,您在哪里锁定了互斥锁?

最新更新