在此处的此链接中,在增量函数中,条件变量在实际增加计数(从零开始)之前发出信号。增加计数后是否不应该调用信号?或者,直到在 increment_function 中释放互斥锁后,decrement_count 函数中的等待调用才会返回?
pthread_mutex_t count_lock;
pthread_cond_t count_nonzero;
unsigned count;
decrement_count()
{
pthread_mutex_lock(&count_lock);
while (count == 0)
pthread_cond_wait(&count_nonzero, &count_lock);
count = count - 1;
pthread_mutex_unlock(&count_lock);
}
increment_count()
{
pthread_mutex_lock(&count_lock);
if (count == 0)
pthread_cond_signal(&count_nonzero);
count = count + 1;
pthread_mutex_unlock(&count_lock);
}