https://github.com/bcuff/asyncprimitives
Async concurrency primitives for .NET
https://github.com/bcuff/asyncprimitives
Last synced: about 1 year ago
JSON representation
Async concurrency primitives for .NET
- Host: GitHub
- URL: https://github.com/bcuff/asyncprimitives
- Owner: bcuff
- License: other
- Created: 2014-04-02T22:54:35.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2018-05-31T20:10:19.000Z (about 8 years ago)
- Last Synced: 2025-04-12T03:59:55.246Z (about 1 year ago)
- Language: C#
- Size: 46.9 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
AsyncPrimitives
===============
Async concurrency primitives for .NET
## Async Semaphore
### Methods
#### Wait
`public Task Wait()`
Waits until the count is greater than 0 then enters the semaphore. Decrements the count upon entry.
#### WaitAndRelease
`public Task WaitAndRelease()`
Waits (as above) then returns an `IDisposable`. When the return value is disposed `void Release()` is called.
#### Release
`public void Release(int count = 1)`
Increments the count by the specified number and releases the corresponding number of waiters (if there are any waiters).
```c#
public class SomeClass
{
private readonly _semaphore = new AsyncSemaphore(3 /* starting count */);
public async Task DoSomethingAsync()
{
using(await _semaphore.WaitAndRelease())
{
// only 3 callers can run this at a time
// ...
}
}
}
```
## Async ReaderWriterLock
```c#
_gate = new AsyncReaderWriterLock();
public async Task ModifyResource()
{
using(await _gate.OpenWriter())
{
// ... do something to modify a protected resource ...
}
}
public async Task GetResource()
{
using(await _gate.OpenReader())
{
// ... do something to obtain a protected resource ...
}
}
```
## AsyncLazy<T>
```c#
var lazy = new AsyncLazy(async () =>
{
using(var client = new WebClient())
{
return await client.DownloadStringTaskAsync("http://google.com");
}
});
// first time actually downloads http://google.com
var html = await lazy;
// second time gets it from memory
var html2 = await lazy;
```