Part 6 of 7 · 4 min read
Shutdown
Stopping cleanly
In plain words: When the producers have no more items, consumers waiting in get would sleep for ever. Something must tell them to stop. The usual way is a stop marker (also called a poison pill): a special value that the producer puts in the queue, one for each consumer. A consumer that takes it exits.
#define STOP (-1)
/* producer, at the end: */ for (int i = 0; i < nconsumers; i++) put(STOP);
/* consumer, in its loop: */ int v = get(); if (v == STOP) break;
The test: one producer put the numbers 1 to 1,000 and then two stop markers, and two consumers added up what they received:
consumers took 515 + 485 = 1000 items, sum 500500 (expected 500500)
consumers took 642 + 358 = 1000 items, sum 500500 (expected 500500)
All 1,000 items were taken exactly once (the sum of 1 to 1,000 is 500,500), and both consumers ended. The split between the two (515 and 485, then 642 and 358) differs in each run: the scheduler decides, and the program must not depend on it.
Because the markers are placed after all real items in a first-in, first-out queue, each consumer sees all the items before its marker. With several producers, the markers are put only after all producers have finished (for example, by the main thread after joining them), or a counter makes the last producer send them.
Other shutdown designs exist: a shared done flag tested under the mutex, with a broadcast to wake sleepers; closing the queue, so that get returns an error when the queue is empty and closed; or thread cancellation, which is hard to use safely and is usually avoided.
Stop marker
A stop marker (poison pill) is a special item that a producer places in a queue to tell a consumer that no more items will follow, so that the consumer can leave its loop and end.
Example: the value -1 put once for each of two consumers after the 1,000 real items makes both consumers return.
Common mistake
Putting only one stop marker for several consumers. One consumer ends, and the others wait for ever. Put one for each, or have each consumer pass the marker on.
Interview angle
Answer in your head first, then open it.
What is the producer-consumer problem?
Coordinating threads that fill and empty a bounded shared buffer.
How do you solve it with semaphores?
empty counts free slots, full counts items, and a mutex protects the indices.
Why must the semaphore wait come before the mutex lock?
Otherwise a producer holding the mutex can wait for a slot that a consumer cannot free: a deadlock.
How do you solve it with condition variables?
A mutex, a count, and not_full and not_empty, each with a while loop.
How do you stop the consumers?
Stop markers, one for each consumer, or a closed flag with a broadcast.
Check yourself: 1. Why does each consumer need its own stop marker? 2. What if several producers send markers too early? 3. Name two other ways to stop consumers.
Words you will meet
| Word | Meaning |
|---|---|
| Producer | A thread that makes items |
| Consumer | A thread that uses items |
| Bounded buffer | A queue of fixed size shared by both |
| Ring buffer | An array used as a circle with two indices |
| Backpressure | A full buffer slows the producer |
| Deadlock | Threads waiting for each other for ever |
| Stop marker | A special item that means "no more" |
| Batching | Moving several items per lock to cut the cost |
Quick notes
- The producer-consumer problem is the synchronisation problem of coordinating threads that put items into a shared fixed-size buffer and threads that take items out, so that the buffer is never.
- A ring buffer is a fixed-size array used as a queue, with one index at which the next item is put and one from which the next is taken, each.
- A deadlock is a permanent standstill of a group of threads, each blocked while waiting for something that only a different member of the group could provide, so that none of them can ever run again.
- Backpressure is the slowing of a producer by a full bounded buffer, which makes the producer wait until the consumer frees space, so that the rate of production adapts to the rate of consumption and memory use stays bounded.
- A stop marker (poison pill) is a special item that a producer places in a queue to tell a consumer that no more items will follow, so that the consumer can leave its loop and end.