第二个线程没有从 cond_wait 唤醒
Second thread doesn't wake up from cond_wait
我们正在努力为 Game of life 找到一个好的同步。
因此我们有一个打印机线程,目前有两个线程计算接下来要打印的新一代单元格。
计算线程只有在旧版本已经打印出来的情况下才能开始计算新版本的游戏。
因此我们使用 pthread_cond_signal()
来自应该唤醒两个计算线程的打印机线程。
出于某种原因,只有一个线程在 pthread_cond_wait()
唤醒
我们已经尝试使用广播代替信号,但没有任何效果。
这就是我们的打印机线程所做的事情:
field -> printed = true;
//pthread_mutex_unlock(&(field -> print_mutex));
int status = pthread_cond_signal(&(field -> print_signal));
这就是我们的计算线程所做的:
while(!field -> printed){
printf("waiting for print_signal: %d\n", field -> printed);
pthread_mutex_unlock(&(field -> print_mutex));
pthread_cond_wait(&(field -> print_signal), &(field -> print_mutex));
printf("print_signal received: %d\n", field -> printed);
}
printf("print_signal received2: %d\n", field -> printed);
pthread_mutex_unlock(&(field -> print_mutex));
之后计算线程进行计算并等待每个线程完成后再设置字段 -> 打印回 false。
我们觉得我们仍然没有真正理解如何正确使用互斥量。
pthread_cond_signal()
向所有正在等待的线程中的一个发送信号。
来自 Linux 的 pthread_cond_signal
文档:
pthread_cond_signal restarts one of the threads that are waiting on the condition variable cond.
通知所有线程等待使用pthread_cond_broadcast
。
来自 Linux 的 pthread_cond_broadcast
文档:
pthread_cond_broadcast restarts all the threads that are waiting on the condition variable cond.
The pthread_cond_broadcast() function shall unblock all threads currently blocked on the specified condition variable cond.
The pthread_cond_signal() function shall unblock at least one of the threads that are blocked on the specified condition variable cond (if any threads are blocked on cond).
您还想在 调用 pthread_cond_wait()
之前锁定互斥体 。当线程进入等待状态时,互斥锁被隐式解锁。
另请注意,在 pthread_cond_wait()
返回后,互斥锁再次被锁定。
要遵循这个概念,您可以将代码更改为如下所示:
pthread_mutex_lock(&(field -> print_mutex));
while (!field -> printed)
{
pthread_cond_wait(&(field -> print_signal), &(field -> print_mutex));
}
pthread_mutex_unlock(&(field -> print_mutex));
还要确保不要在没有保护的情况下写入标志
pthread_mutex_lock(&(field -> print_mutex));
field -> printed = true;
pthread_cond_signal(&(field -> print_signal));
pthread_mutex_unlock(&(field -> print_mutex));
最后采纳这个建议:在您的真实代码中,为 *all 那些 pthread_*()
调用添加错误检查!
并且 ^2 您确实确保了条件和互斥量已正确初始化,不是吗? ;-)
我们正在努力为 Game of life 找到一个好的同步。
因此我们有一个打印机线程,目前有两个线程计算接下来要打印的新一代单元格。
计算线程只有在旧版本已经打印出来的情况下才能开始计算新版本的游戏。
因此我们使用 pthread_cond_signal()
来自应该唤醒两个计算线程的打印机线程。
出于某种原因,只有一个线程在 pthread_cond_wait()
我们已经尝试使用广播代替信号,但没有任何效果。
这就是我们的打印机线程所做的事情:
field -> printed = true;
//pthread_mutex_unlock(&(field -> print_mutex));
int status = pthread_cond_signal(&(field -> print_signal));
这就是我们的计算线程所做的:
while(!field -> printed){
printf("waiting for print_signal: %d\n", field -> printed);
pthread_mutex_unlock(&(field -> print_mutex));
pthread_cond_wait(&(field -> print_signal), &(field -> print_mutex));
printf("print_signal received: %d\n", field -> printed);
}
printf("print_signal received2: %d\n", field -> printed);
pthread_mutex_unlock(&(field -> print_mutex));
之后计算线程进行计算并等待每个线程完成后再设置字段 -> 打印回 false。
我们觉得我们仍然没有真正理解如何正确使用互斥量。
pthread_cond_signal()
向所有正在等待的线程中的一个发送信号。
来自 Linux 的 pthread_cond_signal
文档:
pthread_cond_signal restarts one of the threads that are waiting on the condition variable cond.
通知所有线程等待使用pthread_cond_broadcast
。
来自 Linux 的 pthread_cond_broadcast
文档:
pthread_cond_broadcast restarts all the threads that are waiting on the condition variable cond.
The pthread_cond_broadcast() function shall unblock all threads currently blocked on the specified condition variable cond.
The pthread_cond_signal() function shall unblock at least one of the threads that are blocked on the specified condition variable cond (if any threads are blocked on cond).
您还想在 调用 pthread_cond_wait()
之前锁定互斥体 。当线程进入等待状态时,互斥锁被隐式解锁。
另请注意,在 pthread_cond_wait()
返回后,互斥锁再次被锁定。
要遵循这个概念,您可以将代码更改为如下所示:
pthread_mutex_lock(&(field -> print_mutex));
while (!field -> printed)
{
pthread_cond_wait(&(field -> print_signal), &(field -> print_mutex));
}
pthread_mutex_unlock(&(field -> print_mutex));
还要确保不要在没有保护的情况下写入标志
pthread_mutex_lock(&(field -> print_mutex));
field -> printed = true;
pthread_cond_signal(&(field -> print_signal));
pthread_mutex_unlock(&(field -> print_mutex));
最后采纳这个建议:在您的真实代码中,为 *all 那些 pthread_*()
调用添加错误检查!
并且 ^2 您确实确保了条件和互斥量已正确初始化,不是吗? ;-)