Skip to content

bmbsqd/AsyncLock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 

Repository files navigation

AsyncLock (core)

Async/Awaitable light, non-blocking lock in C#

Usage

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" );
}

Why?

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.

Alternatives

Gotchas

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.

It's fast and lightweight

Best case

  • 1x Interlocked on enter
  • 1x Interlocked on exit

Worst case:

  • 3x Interlocked on enter
  • 1x Interlocked on exit
  • ConcurrentQueue Enqueue/TryDequeue calls

About

Async/Awaitable non-blocking lock in C#

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published