Logo

dev-resources.site

for different kinds of informations.

Solve the scenario - using Thread synchronization in Dotnet - CountDownEvent

Published at
4/23/2021
Categories
dotnet
threading
synchronization
csharp
Author
debashish2014
Author
13 person written this
debashish2014
open
Solve the scenario - using Thread synchronization in Dotnet - CountDownEvent

Use Case:

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.

Race:

  1. Should be able to add the players
  2. Should be able to start the race
  3. It should inform when race starts
  4. It should inform when race ends

Player:

  1. It should have a name
  2. It should declare the fatigue level. Lower is the fatigue, faster the player will run.
  3. It should notify when player starts running
  4. It should notify when player finishes the race

Scenario 1:
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 !!

Scenario 2:
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

?

Think again

Scroll down for the solution

...

...

...

...

...

...

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.

Scenario 1

Alt Text

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.

Scenario 2

Alt Text

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 !!

threading Article's
30 articles in total
Favicon
Concorrência e paralelismo em Python
Favicon
Navigating Concurrency for Large-Scale Systems
Favicon
Common Java Developer Interview Questions and Answers on multithreading, garbage collection, thread pools, and synchronization
Favicon
Real-time plotting with pyplot
Favicon
A Quick Guide to the Python threading Module with Examples
Favicon
Understanding Threading and Multiprocessing in Python: A Comprehensive Guide
Favicon
I Asked Copilot to Explain Threading in Python to a Dog
Favicon
Introduction to GCD (Grand Central Dispatch)
Favicon
Achieving multi-threading by creating threads manually in Swift
Favicon
Swift Concurrency
Favicon
Python Multithreading: Unlocking Concurrency for Better Performance
Favicon
Choosing the best asynchronous library in Python
Favicon
Two Lines of Code Eluded me for Several Months
Favicon
A Comprehensive Guide to Python Threading: Advanced Concepts and Best Practices
Favicon
Thread synchronisation
Favicon
Rust Learning Note: Multithreading
Favicon
Async vs Threading vs Multiprocessing in Python
Favicon
02.Android Background Task
Favicon
How to handle threads with multiple gunicorn workers to get consistent result
Favicon
Tasks, BackgroundWorkers, and Threads
Favicon
Understanding Task.WhenAll in C#
Favicon
Producer/consumer pipelines with System.Threading.Channels
Favicon
How to auto-refresh Realm inside Android WorkManager
Favicon
Understanding Task in .Net
Favicon
Como resolvemos um bug que afetava 3000 usuários e nos custaria milhões por ano
Favicon
Java Thread Programming (Part 1)
Favicon
So, you want to launch several threads in Python and something does not work?
Favicon
Higher level threading in C++
Favicon
Solve the scenario - using Thread synchronization in Dotnet - CountDownEvent
Favicon
Что в процессе тебе моем?

Featured ones: