Single Producer and Consumer

Learn about this basic concurrency pattern in Golang.

Overview of producer-consumer pattern

The producer-consumer pattern is a concurrency design pattern where one or more producers produce objects that are queued up and consumed by one or more consumers.

The producer-consumer problem is a synchronization problem. The producer creates items and places them in a buffer of a predetermined size. The items in the buffer are removed and consumed.

There are various cases of this pattern:

  • A single producer and a single consumer
  • A single producer and multiple consumers
  • Multiple producers and a single consumer
  • Multiple producers and multiple consumers

Let’s look at the practical implementation of each case one by one.

The single producer-consumer problem

In this case, only one process produces the object, and only one process is responsible for the consumption of the produced object.

Get hands-on with 1200+ tech skills courses.