Uber Ride Problem
Understand how to handle concurrency in C# through the Uber Ride Problem, where threads representing rider parties are synchronized to form safe ride combinations. Learn to use locks, semaphores, and barrier concepts to model and control thread interactions, ensuring only allowed rider groupings proceed concurrently.
We'll cover the following...
Uber Ride Problem
Imagine at the end of a political conference, republicans and democrats are trying to leave the venue and ordering Uber rides at the same time. However, to make sure no fight breaks out in an Uber ride, the software developers at Uber come up with an algorithm whereby either an Uber ride can have all democrats or republicans or two Democrats and two Republicans. All other combinations can result in a fist-fight.
Your task as the Uber developer is to model the ride requestors as threads. Once an acceptable combination of riders is possible, threads are allowed to proceed to ride. Each thread invokes the method seated() when selected by the system for the next ride. When all the threads are seated, any one of the four threads can invoke the method drive() to inform the driver to start the ride.
Solution
First let us model the problem as a class. We'll have two methods one called by a Democrat and one by a Republican to get a ride home. When either one gets a seat on the next ride it'll call the seated() method.
To make up an allowed combination of riders, we'll ...