Study guides · Introductory

Locks and condition variables: protect state and wait for change

Separate mutual exclusion from waiting, then reason about a bounded queue and the scope of its lock.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Protect an invariant, not just a variable
  2. Waiting without blocking the thread that can help
  3. Trace a producer and a consumer
  4. Understand the remaining risks
  5. Exercise with two producers
  6. Ask Logeia about this articleCheck your understanding

Protect an invariant, not just a variable

A lock gives cooperating threads a rule for entering a critical section. The useful question is which relationship must remain true while shared state changes. In a bounded queue, checking that space exists and appending an item must be coordinated. Locking only the append operation leaves a gap in which another producer can use the last slot. Define the invariant first: the queue length must never exceed its capacity.

Waiting without blocking the thread that can help

Consider a queue with capacity three that already contains three items. A producer cannot make progress until a consumer removes something. Busy waiting while holding the queue lock prevents that consumer from acquiring the same lock. A condition variable provides a coordinated wait: release the associated lock while sleeping, then reacquire it before continuing. The condition itself lives in the shared state; the notification merely tells a waiter that rechecking may be worthwhile.

python
from threading import Condition

condition = Condition()
queue = []
capacity = 3
item = "example"
with condition:
    while len(queue) == capacity:
        condition.wait()
    queue.append(item)
    condition.notify()

Trace a producer and a consumer

Producer A observes a full queue and waits. Consumer B removes an item and notifies a waiter. A becomes eligible to run, but producer C may acquire the lock first and fill the space. When A finally reacquires the lock, the queue can be full again. Use a while loop around the predicate, even when the notification was legitimate. Follow one consistent lock discipline for testing the predicate, updating state, and coordinating notification.

Understand the remaining risks

Mutual exclusion does not automatically prevent deadlock, starvation, or excessive waiting. Keep critical sections as short as the invariant permits, and avoid holding a shared lock during slow network requests without a specific reason. If an operation needs several locks, establish an acquisition order. Use structured cleanup so exceptions cannot abandon a held lock. Finally, test both correctness and throughput: reducing lock duration is useful only if the state remains valid across the newly exposed interleavings.

Exercise with two producers

Draw a schedule with one available slot and two waiting producers. Mark which thread owns the lock at every step, then let one producer append an item before the other reacquires the lock. Ask whether the second producer can still proceed. Tracking queue length together with lock ownership is more informative than counting notifications. The exercise also explains why a notification should not be modeled as ownership of a specific free slot.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 18

Understand the systems behind the web · STEP 15

Choose a learning path