Inspired by some pretty cool slides by Mike Acton that came through my twitter feed last week, I implemented a version of the Sleeping Barber problem.
For those not familiar with the problem, it’s a story that illustrates some of the perils associated inter process / inter thread synchronization, wikipedia describes it as:
The analogy is based upon a hypothetical barber shop with one barber, one barber chair, and a number of chairs for waiting customers. When there are no customers, the barber sits in his chair and sleeps. As soon as a customer arrives, he either awakens the barber or, if the barber is cutting someone else’s hair, sits down in one of the vacant chairs. If all of the chairs are occupied, the newly arrived customer simply leaves.
Wikipedia was also kind enough to provide pseudo-code for he boring three semaphore solution so an implementation was straightforward and only took a few minutes.
Here’s the skeleton of the outline
Here’s the snippets of the outline, first 3 semaphores and a counter:
cooperative_semaphore customers_semaphore(0,num_customers,L"C"); cooperative_semaphore barber_semaphore(0,num_barbers,L"B"); cooperative_semaphore seats_semaphore (1,num_seats,L"S"); int free_seat_counter = num_seats;
Brien | 18-Jul-09 at 11:55 am | Permalink
(Sorry, I couldn’t figure out how to email)
Can you please do a post on how to best integrate file or network i/o when using ConcRT?
Thanks
rickmolloy | 20-Jul-09 at 10:45 am | Permalink
Sure, I’ll try to get this during the week. Looks like I also need to repost this blog entry, it got truncated.