DSA Factory
Free Synchronization lessonsSynchronization · Stage 2 · The classic problems · Step 4

Monitors and condition variables

See how a monitor bundles the lock with the data, and why a condition wait belongs in a while loop. About 13 minutes.

Quick check

Why should a thread call wait() inside a while loop that re-checks its condition?

  1. AWhen it wakes, the condition may no longer hold: another thread may have used it, or the wakeup may be spurious
  2. BA loop makes wait() return faster
  3. CBecause wait() doesn't release the lock otherwise
Show the answer

When it wakes, the condition may no longer hold: another thread may have used it, or the wakeup may be spurious. Checking again, in a loop, makes the code correct in all those cases.

Lock it in

4 questions on this idea

Short questions that apply the idea to new cases: trace it, work out a number, put steps in order. A wrong answer explains itself.

Answer them in your browserFree account, no card. Your progress is saved, and the idea comes back in a quick review later.
Sign up free to solve