The producer–consumer model separates generating data from receiving and processing it. Producers and consumers may share a bounded queue, or subscribers may independently react to published events. section 16.4 section 12.1
A multithreaded bounded-queue design coordinates access as follows:
- Producers wait while the queue is full, then enter values into its critical region. section 16.4
- Consumers wait while the queue is empty, then remove values from its critical region. section 16.4
- A condition object lets threads wait for queue state changes and notify waiting producers or consumers. section 16.4
- An end sentinel tells consumers when all producers have finished. section 16.4.2
Bounded queue with multiple threads
A Python implementation creates a BoundedQueue, two producers, and three consumers. Producers enter five values each; consumers remove values asynchronously. A condition protects the queue, while different execution orders produce different output sequences.
Figure 16.8 Simultaneously, multiple producers enter values into the bounded queue and multiple consumers remove values from the queue. The queue—the shared resource—has limited capacity.
Visualizes simultaneous producers and consumers sharing a fixed-capacity queue. section 16.4
Published events
A baseball reporter produces hit events. A logger consumes them immediately, while table and graph generators accumulate events and produce reports after the game. section 12.1


