Skip to content

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

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