Provides async friendly mutex, semaphore, and upgradeable reader-writer lock.
$ dotnet add package AsyncSharp
A collection of async friendly classes for resource control, including AsyncSemaphore, AsyncMutex, and ReadersWriterAsyncLock. All classes provide both synchronous and asynchronous methods that expose both timeouts and cancellation token support.
AsyncSemaphore provides similar functionality to SemaphoreSlim, along with the ability to acquire more than 1 count in a single operation, to release all at once, optional fairness (for both synchronous and asynchronous operations together), and optional disposable acquire and release operations.
var semaphore = new AsyncSemaphore(1, 1, true);
await semaphore.WaitAsync();
try
{
// Your operation
}
finally
{
semaphore.Release();
}
AsyncMutex provides similar functionality to AsyncSemaphore, but only allows for an exclusive acquire of the mutex.
var mutex = new AsyncMutex();
await semaphore.LockAsync();
try
{
// Your operation
}
finally
{
semaphore.Unlock();
}
Provides a readers-writer lock that is both async friendly, allows for optional fairness, optional max number of readers, and optional upgradeable locks.
var readersWriterAsyncLock = new ReadersWriterAsyncLock();
using (var upgradeableLock = await readersWriterAsyncLock.AcquireUpgradeableReaderAsync())
{
// Do operations while holding reader lock
using (var writerLock = await upgradeableLock.UpgradeToWriterAsync())
{
// Do operations while holding writer lock
}
// Finish any operations with reader lock
}27.5K