Producer-consumer 00:00:00 Make a prediction You will trace a bounded queue and distinguish arrival order, retrieval order and completion order. Tea arrives before toast. Two cooks take the jobs in that order. Must tea reach the customer first? No. Taking in FIFO order does not imply finishing in FIFO order when processing times differ. Decide whether the contract promises starts, completions, or displayed results in order. 00:00:31 The log rack at the winter cabin At a counter two order slips fit on a rail. The cashier clips new slips to one end; a cook takes from the other. When both slots are full a third slip cannot be accepted until somebody makes room. Producers and consumers can run at different speeds, but finite buffering requires backpressure. Closing the counter is different from discarding already accepted orders. Producer-consumer is a concurrency pattern. Producers create items, consumers process them, and a shared buffer lies between them. Both sides run at their own speed. Two parts of a program rarely have the same speed. Called directly, the fast part waits for the slow part each time. With a buffer it waits only when the buffer is full. 00:01:18 Buffer two kitchen orders A counter accepts two waiting orders. Tea and toast fill it while the cook is busy. A third order cannot fit yet. The producer waits or reports a full queue under the chosen contract. When the cook takes tea, one slot becomes available for the third order. Track ordering separately from completion: two cooks can take items in arrival order yet finish the shorter order first. Specify which ordering the customer actually needs. Let the cook pause while producers fill both slots. A third order cannot enter. As soon as the cook removes A, one waiting producer can proceed. If the producer drops the queue lock to wake the cook but changes the size outside that guard, the count and contents can disagree. 00:02:06 Drain before stopping At closing time stop accepting new orders. Let workers take existing orders until the queue is empty and closed. If a stop signal jumps ahead of queued work, customers can lose orders already accepted. Test with a queue that is nonempty when closing begins. Stop accepting orders while A and B still wait. The consumer must serve both before reporting end of stream. A sentinel inserted too early can make it exit with B still on the counter. Closing and draining represent two different conditions. A stop item inserted ahead of older orders can make workers leave before serving accepted customers. Test closure with a nonempty queue. If completed outputs must be delivered in original order, add sequence tracking or restrict processing concurrency. A Unix pipe buffers data between a writer and a reader. In blocking mode, a full pipe can suspend the writer and an empty pipe can suspend the reader while a writer is still open. Closing every writing end gives the reader a separate end-of-stream signal after remaining bytes have been consumed. 00:03:15 Try an interview follow-up How would you make it faster? I would measure where workers spend time before changing the queue. If handoffs dominate, batching a small number of items at a time reduces them. If independent keys contend, separate queues may help. I would retest ordering, fairness and latency after each change; higher throughput alone can hide a longer wait for individual items. Two producers write into one bounded queue. The first producer finishes while the second still has data. If the first closes the queue, later items are lost. Make a coordinator close it after all producers finish. Consumers drain accepted items and exit once the closed queue is empty. 00:03:59 What to take with you A bounded buffer stands between producers and consumers. It owns one lock and two conditions. put waits while full, take waits while empty, each in a loop, and each wakes one thread of the other side. The capacity is a decision about waiting time: rate times seconds. What a full buffer means, waiting, refusing or dropping, is a decision too, and it is counted.