Async/Awaitable light, non-blocking lock in C#
First install the nuget package
To create a new lock, you have to new it up
private AsyncLock _lock = new AsyncLock();And to protect a section of code using the lock
using( await _lock ) {
_log.Info( "Inside Lock" );
}Lightweight, fast, no tasks involved in the await process, very little overhead, Interlocked.
Internally uses ConcurrentQueue to hold waiters, but will bypass structure completely if there's nothing to wait for.
- Nito AsyncEx -- http://nitoasyncex.codeplex.com/
- W8 and WP8 -- http://asynclock.codeplex.com/
- System.Threading.SemaphoreSlim
AsyncLock is not reentrant and will deadlock its self when entering the same lock multiple times on same execution path
There's no deadlock monitoring built in.
Best case
- 1x Interlocked on enter
- 1x Interlocked on exit
Worst case:
- 3x Interlocked on enter
- 1x Interlocked on exit
- ConcurrentQueue Enqueue/TryDequeue calls