https://github.com/usercode/asynckeylock
Async Key-based Reader Writer Lock with Cancellation Support
https://github.com/usercode/asynckeylock
csharp dotnet locking
Last synced: 7 months ago
JSON representation
Async Key-based Reader Writer Lock with Cancellation Support
- Host: GitHub
- URL: https://github.com/usercode/asynckeylock
- Owner: usercode
- License: mit
- Created: 2022-07-15T12:24:42.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-26T15:48:27.000Z (over 1 year ago)
- Last Synced: 2024-11-29T05:56:55.650Z (7 months ago)
- Topics: csharp, dotnet, locking
- Language: C#
- Homepage:
- Size: 54.7 KB
- Stars: 5
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Async Key-based Reader Writer Lock with Cancellation Support
[](https://opensource.org/licenses/MIT)
[](https://www.nuget.org/packages/AsyncKeyLock)- 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.## How to use it
```csharp
//async lock
AsyncLock asyncLock = new AsyncLock();//async lock with key
AsyncLock asyncLock = new AsyncLock();//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 lockif (..)
{
r1.UseWriterLockAsync(async () => { /*use writer lock here*/ });
}//continue with reader lock
}
```