1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. Why must a wait on a std::condition_variable be wrapped in…

Added 30 Nov -0001

Why must a wait on a std::condition_variable be wrapped in a predicate loop?

  1. Predicates are evaluated outside the lock, so the result may become stale
  2. Wake-ups can occur without a notify, so the condition must be rechecked Correct answer
  3. The mutex is released before the predicate runs and may be reacquired late
  4. notify_one can be lost when nobody is waiting, so the wait must poll

Correct answer

B. Wake-ups can occur without a notify, so the condition must be rechecked

16 views

MCQSEXAM

Explanation

condition_variable waits may return spuriously, without any corresponding notify, so the predicate has to be re-evaluated in a loop; the overload taking a predicate does this for you. The mutex must also be held while the predicate is checked, to close the window in which a notify could be lost between the test and the sleep. A notification issued with no waiter is simply not remembered, which is why the state, not the signal, must be the thing that is checked.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs