SkillAgentSearch skills...

AsyncLock

Async/Awaitable non-blocking lock in C#

Install / Use

/learn @bmbsqd/AsyncLock
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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
View on GitHub
GitHub Stars18
CategoryDevelopment
Updated8mo ago
Forks2

Languages

C#

Security Score

82/100

Audited on Jul 16, 2025

No findings