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.
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.