Async Key-based Reader Writer Lock with Cancellation Support
![NuGet](https://camo.githubusercontent.com/60b6384595d50f4c64fe732f8248fdc42134c1cf1a087205d11ffb4723e736e3/68747470733a2f2f696d672e736869656c64732e696f2f6e756765742f762f4173796e634b65794c6f636b2e7376673f7374796c653d666c61742d737175617265)
- Can create reader and writer locks (with key).
- A reader lock allows concurrent access for read-only operations.
- A writer lock allows exclusive access for operations.
- It based internally on TaskCompletionSources which will be queued.
//async lock
AsyncLock asyncLock = new AsyncLock();
//async lock with key
AsyncLock<string> asyncLock = new AsyncLock<string>();
//acquire reader lock
var d1 = await asyncLock.ReaderLockAsync("123");
//release reader lock
d1.Dispose();
//set timeout
using CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
//acquire writer lock
using var d2 = await asyncLock.WriterLockAsync("123", cts.Token);
//use writer lock scope to interrupt long running reader lock
using (ReaderReleaser r1 = await asyncLock.ReaderLockAsync())
{
//use reader lock
if (..)
{
r1.UseWriterLockAsync(async () => { /*use writer lock here*/ });
}
//continue with reader lock
}