Producer-consumer¶
Category: Communication · Status: stub · Lessons: chapter 05, Message passing (planned)
One line: One or more tasks make work items and one or more take them from a shared queue, each side running at its own speed.
Also called: producer-consumer problem, bounded-buffer problem.
How it connects¶
flowchart LR
n_bounded_channel["Buffered and bounded channels"]
n_producer_consumer["Producer-consumer"]
n_task_queue["Task queue"]
n_producer_consumer -->|uses| n_bounded_channel
n_task_queue -->|uses| n_producer_consumer
classDef center stroke-width:3px
class n_producer_consumer center
classDef outside stroke-dasharray: 4 3
class n_bounded_channel,n_task_queue outside
- Is built on: Buffered and bounded channels
- Is used by: Task queue
- See also: Classic synchronization problems, Worker pool
In each language¶
| Rust | threads sharing an mpsc ↗ channel, with a cloned Sender for each producer |
| Go | goroutines sharing a buffered channel ↗ |
| Java | BlockingQueue ↗, which its docs say is designed primarily for producer-consumer queues |
| Python | queue.Queue ↗; task_done and join let a producer wait until every item has been processed |
| C# | System.Threading.Channels ↗, data structures for passing data between producers and consumers asynchronously |
| Kotlin | a coroutine built with produce is a channel producer ↗ that consumers iterate over |
Where to read more¶
- In a sibling library: Go: A buffered channel is a bounded queue ↗
- In a sibling library: Go: A worker pool ↗
- In the books: Kotlin Coroutines by Tutorials, Filip Babić, Nishant Srivastava — ch. 13, 'Producer & Actors'
- In the books: Effective Concurrency in Go, Burak Serdar — ch. 4, 'Some Well-Known Concurrency Problems' → 'The producer-consumer problem'
- In the books: Java Concurrency in Practice, Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, Doug Lea — ch. 5, 'Building Blocks' → 'Blocking Queues and the Producer-consumer Pattern'
- In the books: Pro Asynchronous Programming with .NET, Richard Blewett, Andrew Clymer — ch. 10, 'TPL Dataflow' → 'Producer and Consumer Revisited'
- In the books: The Art of Multiprocessor Programming, Maurice Herlihy, Nir Shavit — ch. 1, 'Introduction' → 'The Producer-Consumer Problem'
- In the books: The Little Book of Semaphores, Allen B. Downey — ch. 4, 'Classical synchronization problems' → 'Producer-consumer problem'
- Reference: Wikipedia: Producer–consumer problem ↗