Consider this hypothetical scenario. We need to simulate a Marathon Race. So, we have two entities Race & Player
Following are the requirements of each entity.
- Should be able to add the players
- Should be able to start the race
- It should inform when race starts
- It should inform when race ends
- It should have a name
- It should declare the fatigue level. Lower is the fatigue, faster the player will run.
- It should notify when player starts running
- It should notify when player finishes the race
When race starts it should wait for all the players to finish the race. Race will only end after that.
All players have a consolation price !!
When race starts it should wait for the first three winners !! Race ends after it.
Only first three winners will get a price!! Sorry about other folks.
Take a pause
Think about the solution
Scroll down for the solution
Run the below code in Linqpad (https://www.linqpad.net/), to see it action. You can create a console project in VS and copy this code as well.
Make sure you press Ctrl+Shift+M, and add the namespace imports i.e. System.Threading & System.Threading.Tasks
We will be using TPL (Task Parallel Library) and Thread Synchronization constructs to solve the problem.
If you see the output above, the player who is having the lowest fatigue level comes first and one who comes last is having the highest fatigue.
In this solution we are using Task.WaitAll() to wait for all the players to finish the race, before we end it.
In this solution we are using CountDownEvent. When a player finishes the race it signal's the CountDownEvent by calling the Signal function/method on it. When it get's the signal, it's counter is decremented by 1. When first 3 players finishes the race, the counter would have been decremented thrice, so the counter would be 0 at this point. Since the Race is waiting on CountDownEvent, as soon as it's counter reaches 0, the race would end.
I hope you have understood how to use synchronization to solve these type of problems.
Happy Coding !!