我正试着把我的头集中在线程条件变量上。我看到了一些使用pthread_cond_wait和pthread_cond_signal的代码示例,它们都如下所示:
while (condition)
{
// Assume that the mutex is locked before the following call
pthread_cond_wait(&cond, &mutex);
}
是否有理由在这种情况下使用while循环?为什么不只用一个if语句呢?
假设你有这段代码
pthread_mutex_lock(&cam->video_lock);
while(cam->status == WAIT_DISPLAY) // <-- Why is this a 'while' and not an 'if'?
pthread_cond_wait(&cam->video_cond, &cam->video_lock);
pthread_mutex_unlock(&cam->video_lock);
我的问题是,你为什么需要在这里循环一下。pth
我试图了解互斥变量和条件变量之间的区别,并对下面的代码感到困惑。
// Lock mutex and then wait for signal to relase mutex
pthread_mutex_lock( &count_mutex );
/*Wait while functionCount2() operates on count
mutex unlocked if condition varialbe in functionCount2() signaled. <-- so then why call pthread_mutex_unlock() 3 lines l