Soenneker.Asyncs.Locks 4.0.52

Prefix Reserved
dotnet add package Soenneker.Asyncs.Locks --version 4.0.52
                    
NuGet\Install-Package Soenneker.Asyncs.Locks -Version 4.0.52
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Soenneker.Asyncs.Locks" Version="4.0.52" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Asyncs.Locks" Version="4.0.52" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Asyncs.Locks" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Soenneker.Asyncs.Locks --version 4.0.52
                    
#r "nuget: Soenneker.Asyncs.Locks, 4.0.52"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Soenneker.Asyncs.Locks@4.0.52
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Soenneker.Asyncs.Locks&version=4.0.52
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Asyncs.Locks&version=4.0.52
                    
Install as a Cake Tool

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

Soenneker.Asyncs.Locks

The fastest .NET async lock

This library provides a single primitive: AsyncLock.

Design goal

AsyncLock is built to be the fastest possible correct mutex for real-world .NET systems.

It provides the following guarantees:

Cancellation-safe
  • Fully supports cancellation before acquisition and while waiting for both async and sync callers.
  • Cancelled waiters are removed immediately, never resumed, and never leaked � with zero impact on the fast path.
Unified async + sync locking
  • Async and synchronous callers share the same mutex.
  • Ordering is preserved without adapters, wrappers, or duplicated synchronization primitives.
Performance (by design)
  • Uncontended acquisition is as close to a single atomic operation as possible
  • No allocations, tasks, or state machines unless contention occurs
  • Cancellation and disposal logic are completely excluded from the fast path
  • Deterministic behavior under contention

Installation

dotnet add package Soenneker.Asyncs.Locks

Usage

Async

await using (await _lock.Lock(ct))
{
    // critical section
}

Sync

using (_lock.LockSync())
{
    // critical section
}

Try-lock

if (_lock.TryLock(out var releaser))
{
    using (releaser)
    {
        // critical section
    }
}

Benchmarks

Async lock acquisition

Method Mean Error StdDev Median Ratio Allocated
Soenneker.Asyncs.Lock 10.06 ns 0.212 ns 0.393 ns 10.01 ns baseline -
SemaphoreSlim 19.17 ns 0.406 ns 0.360 ns 19.10 ns 1.91x slower -
Nito.AsyncEx.AsyncLock 55.32 ns 1.078 ns 2.645 ns 54.81 ns 5.51x slower 320 B

Synchronous lock acquisition

Method Mean Error StdDev Median Ratio Allocated
Soenneker.Asyncs.Lock 8.09 ns 0.179 ns 0.314 ns 8.03 ns baseline -
SemaphoreSlim 19.49 ns 0.403 ns 0.727 ns 19.09 ns 2.41x slower -
Nito.AsyncEx.AsyncLock 48.43 ns 1.005 ns 2.915 ns 48.05 ns 6.00x slower 320 B
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (8)

Showing the top 5 NuGet packages that depend on Soenneker.Asyncs.Locks:

Package Downloads
Soenneker.Utils.AsyncSingleton

An externally initializing singleton that uses double-check asynchronous locking, with optional async and sync disposal

Soenneker.Utils.BackgroundQueue

A high-performance background Task/ValueTask queue

Soenneker.Blazor.Utils.Session

A Blazor utility for automatic navigation after JWT expiration

Soenneker.Dictionaries.SingletonKeys

An externally initializing singleton dictionary that uses double-check asynchronous locking, with optional async and sync disposal

Soenneker.Asyncs.Initializers

A lightweight, async-safe, allocation-free one-time initialization gate. Ensures a given asynchronous initialization routine runs exactly once, even under concurrent callers, with support for cancellation, safe publication, and disposal.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.52 15,561 6/6/2026
4.0.51 13,632 6/6/2026
4.0.50 215 6/5/2026
4.0.49 4,781 6/5/2026
4.0.48 46 6/5/2026
4.0.47 240,174 4/23/2026
4.0.46 7,822 4/23/2026
4.0.45 2,816 4/23/2026
4.0.44 5,695 4/23/2026
4.0.41 242,082 3/13/2026
4.0.40 9,554 3/13/2026
4.0.39 279 3/13/2026
4.0.38 1,042 3/12/2026
4.0.37 242 3/12/2026
4.0.36 249 3/12/2026
4.0.35 1,021 3/12/2026
4.0.34 581 3/12/2026
4.0.33 72,777 3/11/2026
4.0.32 8,573 3/11/2026
4.0.31 5,473 3/11/2026
Loading failed

Update dependency Soenneker.Queues.Intrusive.ValueMpsc to 4.0.23 (#133)