Classic Synchronization Problems (Producer-Consumer)

A Shared Buffer Between Two Kinds of Threads

The Producer-Consumer problem is a classic scenario used to illustrate synchronization: a "producer" thread generates data and places it into a shared, fixed-size buffer, while a "consumer" thread removes and processes that data - and both must be carefully coordinated.

Producer  ---->  [ Shared Buffer (fixed size) ]  ---->  Consumer

Two Distinct Problems That Must Both Be Prevented

  • Buffer overflow - the producer must not add data when the buffer is already completely full.
  • Buffer underflow - the consumer must not try to remove data when the buffer is completely empty.

Solving It With Three Semaphores

Semaphore empty = N     // counts empty slots (starts full, at N)
Semaphore full  = 0     // counts filled slots (starts empty)
Semaphore mutex = 1     // ensures exclusive access to the buffer itself

Producer:                          Consumer:
  wait(empty)                        wait(full)
  wait(mutex)                        wait(mutex)
    add item to buffer                 remove item from buffer
  signal(mutex)                      signal(mutex)
  signal(full)                       signal(empty)

How This Precisely Prevents Both Overflow and Underflow

The producer's wait(empty) blocks it automatically once the buffer is completely full (no empty slots left), and the consumer's wait(full) blocks it automatically once the buffer is completely empty (no filled slots left) - the counting semaphores (covered in the earlier semaphore note) directly track exactly how much room and how much data currently exists.

Why a Separate Mutex Is Still Necessary, Even With Two Other Semaphores

The empty and full semaphores manage how many items exist, but adding or removing an item from the buffer is itself a critical section (covered earlier in this section) - the mutex semaphore is what specifically ensures the producer and consumer never corrupt the buffer's internal state by touching it at exactly the same moment.

Why This Classic Problem Remains So Widely Taught

Nearly every real-world scenario involving one thread generating work and another consuming it - a background task queue, a logging system, a streaming pipeline - is, at its core, a variation of exactly this same producer-consumer pattern.

Written by Lorens Mishra

Software Engineer Notes Management System Administrator

Discussion

0 comments
Sign in to join the discussion.

No comments yet. Be the first to say something.