45 The Senate Bus problem
This problem was originally based on the Senate bus at Wellesley College. Riders come to a bus stop and wait for a bus. When the bus arrives, all the waiting riders invoke boardBus, but anyone who arrives while the bus is boarding has to wait for the next bus. The capacity of the bus is 50 people; if there are more than 50 people waiting, some will have to wait for the next bus.
When all the waiting riders have boarded, the bus can invoke depart. If the bus arrives when there are no riders, it should depart immediately.
Puzzle: Write synchronization code that enforces all of these constraints.
45.1 Bus problem hint
Here are the variables I used in my solution:
mutex protects riders, which keeps track of how many riders are waiting; multiplex makes sure there are no more than 50 riders in the boarding area.
Riders wait on bus, which gets signaled when the bus arrives. The bus waits on allAboard, which gets signaled by the last student to board.
45.2 Bus problem solution #1
Here is the code for the bus. Again, we are using the “Pass the baton” pattern.
When the bus arrives, it gets mutex, which prevents late arrivals from entering the boarding area. If there are no riders, it departs immediately. Otherwise, it signals bus and waits for the riders to board.
Here is the code for the riders:
The multiplex controls the number of riders in the waiting area, although strictly speaking, a rider doesn’t enter the waiting area until she increments riders.
Riders wait on bus until the bus arrives. When a rider wakes up, it is understood that she has the mutex. After boarding, each rider decrements riders. If there are more riders waiting, the boarding rider signals bus and pass the mutex to the next rider. The last rider signals allAboard and passes the mutex back to the bus.
Finally, the bus releases the mutex and departs.
Puzzle: can you find a solution to this problem using the “I’ll do it for you” pattern?
45.3 Bus problem solution #2
Grant Hutchins came up with this solution, which uses fewer variables than the previous one, and doesn’t involve passing around any mutexes. Here are the variables:
waiting is the number of riders in the boarding area, which is protected by mutex. bus signals when the bus has arrived; boarded signals that a rider has boarded.
Here is the code for the bus.
The bus gets the mutex and holds it throughout the boarding process. The loop signals each rider in turn and waits for her to board. By controlling the number of signals, the bus prevents more than 50 riders from boarding.
When all the riders have boarded, the bus updates waiting, which is an example of the “I’ll do it for you” pattern.
The code for the riders uses two simple patterns: a mutex and a rendezvous.
Challenge: if riders arrive while the bus is boarding, they might be annoyed if you make them wait for the next one. Can you find a solution that allows late arrivals to board without violating the other constraints?