Study guides · Intermediate

Design condition predicates including shutdown

Derive waiting and shutdown from capacity, contents and closure, then drain accepted work and collect two consumers.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Decide from current predicates, not notification counts
  2. Derive producer and consumer waiting conditions
  3. Return to the same lock after waiting
  4. Run two consumers through closing and draining
  5. Add closure to the state table
  6. Separate fairness and wakeup cost
  7. Keep check-and-change inside one invariant boundary
  8. Keep termination markers outside ordinary data
  9. Exercise: decide shutdown behavior from state and operation
  10. Ask Logeia about this articleCheck your understanding

Decide from current predicates, not notification counts

Several consumers wait on one work queue. A notification does not reserve an item: another consumer may acquire the lock first and remove the final item. Building on the basic condition-variable lesson, this guide adds capacity and shutdown semantics. Write waiting conditions as predicates over shared state and evaluate them under the same lock that protects changes.

State consists of items, capacity and closed. Queue length stays between zero and capacity, and closing rejects new work. Already accepted items are drained before consumers exit. Discard-on-close would be another valid policy but is not this implementation's promise. Termination follows the combination of closed and current contents, not how many notifications occurred. The shutdown contract belongs in the state definition before thread code is added.

Derive producer and consumer waiting conditions

Put waits only while the queue is full and not closed. On waking it rechecks and rejects if closure occurred, even if space also became available. Get waits only while empty and not closed. If it then finds no items, closure means it can return termination. If closed with queued items, it still drains them under the chosen policy.

Waiting predicates must account for exit and failure paths, not only ordinary readiness. A producer waiting on fullness alone may remain stuck after shutdown when no consumer will free space. A consumer waiting on emptiness alone can sleep forever after work admission stops. Capacity and closure must be examined together under one state-protection rule. Splitting their checks across unrelated locks destroys the atomic state decision the protocol relies on.

Shared stateConsumerProducer
Empty, openWait in whileInsert if capacity permits
Full, openCan removeWait in while
Closed, items remainDrain remaining itemsReject
Closed, emptyReturn terminationReject

Return to the same lock after waiting

Condition.wait releases the lock while waiting and reacquires it before returning. Busy-checking while retaining the lock prevents the other participant from changing the state. Manually unlocking and later sleeping on an unrelated signal introduces a separate lost-notification window. Use the condition variable's protocol to connect predicate evaluation with waiting.

Recheck in a while loop regardless of why the wait returned. State can change between notification and lock reacquisition. This implementation notifies all waiters and lets each reevaluate its predicate. Separate producer and consumer conditions may reduce unnecessary wakes, but require a correct mapping from state changes to affected waiters. An optimization that wakes only one thread must not leave other shutdown waiters asleep permanently.

Run two consumers through closing and draining

The code starts two consumers, puts seven and eight into a capacity-two queue, then closes admission. Consumers repeatedly get work and return on the None termination marker, so work items must not themselves be None. Bounded joins collect both threads. Assertions check each accepted item was processed once and post-close put raises RuntimeError. No fixed sleep guesses another thread's readiness.

This run covers shutdown and draining but does not enumerate every OS interleaving. The rationale for rechecking after a wake comes from the predicate argument and existing notification exercise. Capacity is fixed positive in the example. A reusable API must additionally define invalid capacities, caller exceptions and the permitted item domain. Do not silently treat the example's implicit value restrictions as a complete generic queue contract.

python
from collections import deque
from threading import Condition,Thread
class Queue:
    def __init__(self,capacity):self.items=deque();self.capacity=capacity;self.closed=False;self.cv=Condition()
    def put(self,item):
        with self.cv:
            while len(self.items)==self.capacity and not self.closed:self.cv.wait()
            if self.closed:raise RuntimeError('closed')
            self.items.append(item);self.cv.notify_all()
    def get(self):
        with self.cv:
            while not self.items and not self.closed:self.cv.wait()
            if not self.items:return None
            item=self.items.popleft();self.cv.notify_all();return item
    def close(self):
        with self.cv:self.closed=True;self.cv.notify_all()
q=Queue(2);results=[]
def consume():
    while True:
        item=q.get()
        if item is None:return
        with q.cv:results.append(item)
workers=[Thread(target=consume,daemon=True) for _ in range(2)]
for worker in workers:worker.start()
q.put(7);q.put(8);q.close()
for worker in workers:worker.join(5);assert not worker.is_alive()
assert sorted(results)==[7,8]
try:q.put(9)
except RuntimeError:pass
else:raise AssertionError('closed queue accepted work')
print('two consumers stopped; queued items drained exactly once; post-close put rejected')

Add closure to the state table

List open-empty, open-partial, open-full, closed-with-items and closed-empty states. Put waits only in open-full; get waits only in open-empty. Closed-with-items still permits draining, while closed-empty terminates. A state table makes every return and waiting path more visible than a list of notification calls alone.

Close changes the admission flag under the lock and wakes all relevant waiters. It does not forcibly cancel work already removed and executing. Acknowledgment, retries and requeuing failed business jobs require additional protocols omitted by the trivial seven/eight work example. Admission stop, draining, in-flight cancellation and thread collection are four different shutdown boundaries. A product should expose the boundary it actually implements instead of giving all four the same ambiguous stop label.

Separate fairness and wakeup cost

Wake order is not automatically lock-acquisition order, and a condition variable does not guarantee equal work distribution. One consumer may process both items while the other processes none. The contract requires every accepted item once and both threads terminated, not an accidental one-item-per-consumer split. Fairness needs its own policy and tests.

Waking everyone can incur redundant predicate checks, so performance comparisons may examine consumer count, workload, waiting and critical-section length. Preserve the shutdown contract while comparing. Do not hide termination and collection outside the measurement and ignore leftover threads merely to improve a throughput number. Start with understandable predicates and use observed costs to motivate narrower notification strategies without changing which waiters must eventually make progress.

Keep check-and-change inside one invariant boundary

If a producer checks length, releases the lock and appends later, two producers can both observe the last free slot and overfill the queue. Before discussing notifications, keep the check and change inside one invariant-preserving critical section. Put reevaluates and appends while holding the same condition lock.

Get similarly checks and removes under one boundary. Perform expensive external work after removal rather than retaining the queue lock throughout communication. Requeuing failed work introduces additional capacity and close semantics; the two-item result-list example does not implement that policy. Distinguish protected queue state from business work performed outside it. Name which invariant each lock protects, so extending the consumer does not accidentally make unrelated slow operations block every producer and waiter.

The queue drains existing items after closure. Immediate discard would be a different contract and would require different expected histories.

Keep termination markers outside ordinary data

Get returns None for closed-and-empty, so None is excluded from ordinary work items. An API accepting every Python value needs a dedicated sentinel or a distinct result type. A collision in value meaning can make a consumer terminate on real work even when synchronization itself is correct. Data-domain design and locking correctness are separate requirements.

An exception-based closure interface likewise needs to distinguish temporarily empty from permanently closed and specify what callers catch. Enqueuing one sentinel is another protocol whose multi-consumer termination behavior must be checked. The shared closed flag used here is not equivalent to distributing one ordinary queue item per consumer. Put value, state, notification and consumer termination in separate columns to expose which event actually ends each participant.

Exercise: decide shutdown behavior from state and operation

If a closed queue still holds one item, this drain-on-close policy returns that item. The next empty get returns termination. Put is rejected after closure even if space exists. The same closed flag does not dictate one identical result for every operation; combine it with contents and the operation's purpose.

If two consumers wait on an empty queue and close wakes only one, the other may sleep indefinitely with no future work or notification. Closure changes every waiter's exit condition, so all affected waiters need an opportunity to reevaluate. Finally draw a wake followed by another consumer taking the last item before lock reacquisition. That explains why replacing while with if is unsafe: notification is an invitation to inspect the predicate, not a reservation of the resource.

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