https://github.com/formix/formix.semaphore
.NET Standard implementation of an awaitable semaphore.
https://github.com/formix/formix.semaphore
Last synced: 11 months ago
JSON representation
.NET Standard implementation of an awaitable semaphore.
- Host: GitHub
- URL: https://github.com/formix/formix.semaphore
- Owner: formix
- License: mit
- Created: 2018-08-08T20:32:05.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-09-18T22:18:28.000Z (over 7 years ago)
- Last Synced: 2025-06-18T04:47:32.864Z (12 months ago)
- Language: C#
- Homepage:
- Size: 71.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Formix.Semaphore
In-process .NET Standard implementation of an awaitable semaphore.
# Usage
A semaphore is an abstraction on a resource that is available in limited
quantity. For example, you could have a subscription to an online service
that limits the number of simultaneous connection from your account to two.
To make sure you don't use more connection than you should at any time, run
your code using the semaphore's `Execute` method.
```c#
var semaphore = Semaphore.Initialize("connections", 2);
var task1 = semaphore.Execute(() =>
{
Console.WriteLine("Task 1 started.");
Task.Delay(250).Wait();
Console.WriteLine("Task 1 done.");
});
var task2 = semaphore.Execute(() =>
{
Console.WriteLine("Task 2 started.");
Task.Delay(500).Wait();
Console.WriteLine("Task 2 done.");
});
var task3 = semaphore.Execute(() =>
{
Console.WriteLine("Task 3 started.");
Task.Delay(350).Wait();
Console.WriteLine("Task 3 done.");
});
Task.WaitAll(task1, task2, task3);
```
The output should look something like that:
```
Task 1 started.
Task 2 started.
Task 1 done.
Task 3 started.
Task 2 done.
Task 3 done.
```