Barber Shop
Explore how to coordinate multiple threads in Ruby by solving the barbershop concurrency problem. Understand how to use mutexes and semaphores to manage waiting customers, handle the barber's sleep and wake cycles, and ensure safe synchronization between customer and barber threads. This lesson guides you through designing and implementing a practical concurrency solution step-by-step.
We'll cover the following...
Barber Shop
A similar problem appears in Silberschatz and Galvin's OS book, and variations of this problem exist in the wild.
A barbershop consists of a waiting room with n chairs, and a barber chair for giving haircuts. If there are no customers to be served, the barber goes to sleep. If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop. If the barber is busy, but chairs are ...