The shop
A barber shop has one barber chair and a waiting room with a fixed number of chairs. When there are no customers the barber sleeps. A customer who arrives wakes the barber if asleep, sits in a waiting chair if one is free, or leaves if the room is full.
The synchronisation
This is a bounded queue with a worker that must be woken. It needs:
- A semaphore counting waiting customers, used to wake the barber.
- A semaphore the barber posts to signal the chair is ready.
- A mutex guarding the count of free waiting chairs.
The tricky races
The classic bug is a customer arriving exactly as the barber checks for work. Without careful ordering the customer may think the barber is busy while the barber thinks the room is empty, and both sleep forever. Updating the shared count under the mutex before posting the wake semaphore closes this gap.
The full room case models load shedding, a customer who cannot be served is turned away rather than queued unbounded.
Key idea
The sleeping barber models a single worker draining a bounded queue, where careful semaphore ordering avoids the lost wakeup and a full room sheds load.