Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you need a while loop while waiting for a condition variable

Tags:

Say you have this code

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

My question is, why do you need a while loop here. Wouldn't pthread_cond_wait just wait until the signalling thread signals cam_video_cond? OK, I know you might have a case where cam->status is not equal to WAIT_DISPAY when pthread_cond_wait is called, but in that case you could just check it through an if condition rather than using while.

Am I missing something here? My understanding of pthread_cond_wait is that it just waits for infinite if cam_video_cond is not signalled. Moreover, it unlocks the cam_video_lock mutex when called, but when the condition is signalled, before returning, it relocks cam_video_lock. Am I right?

like image 264
MetallicPriest Avatar asked Oct 14 '11 10:10

MetallicPriest


People also ask

Why do we use while statement instead of IF statement in a wait on a condition variable?

It is recommended that a condition wait be enclosed in a "while loop" that checks the predicate. Additionaly: According to difficulty with creating awakening conditions, which might be unpredictable in multithread systems, threads may wake up whenever for whatever reason.

Why should you check condition for waiting in a loop?

To guarantee liveness, programs must test the while loop condition before invoking the wait() method. This early test checks whether another thread has already satisfied the condition predicate and sent a notification. Invoking the wait() method after the notification has been sent results in indefinite blocking.

What does condition variable wait do?

condition_variable::wait wait causes the current thread to block until the condition variable is notified or a spurious wakeup occurs, optionally looping until some predicate is satisfied (bool(stop_waiting()) == true).

Does condition variable need mutex?

You need condition variables, to be used with a mutex (each cond. var. belongs to a mutex) to signal changing states (conditions) from one thread to another one. The idea is that a thread can wait till some condition becomes true.


2 Answers

It is recommended that all threads check the condition after returning from pthread_cond_wait because there are several reasons the condition might not be true. One of these reasons is a spurious wakeup; that is, a thread might get woken up even though no thread signalled the condition.

Source : Spurious wakeup

like image 117
Prince John Wesley Avatar answered Oct 24 '22 03:10

Prince John Wesley


Spurious wakeups are one reason, but legitimate but extraneous wakeups are another.

Consider:

  1. You put a job on a queue.

  2. You signal the condition variable, waking thread A.

  3. You put a job on a queue.

  4. You signal the condition variable, waking thread B.

  5. Thread A gets scheduled, does the first job.

  6. Thread A finds the queue non-empty and does the second job.

  7. Thread B gets scheduled, having been woken, but finds the queue still empty.

like image 28
David Schwartz Avatar answered Oct 24 '22 01:10

David Schwartz