AsyncSemaphore 2.0.0
dotnet add package AsyncSemaphore --version 2.0.0
NuGet\Install-Package AsyncSemaphore -Version 2.0.0
<PackageReference Include="AsyncSemaphore" Version="2.0.0" />
<PackageVersion Include="AsyncSemaphore" Version="2.0.0" />
<PackageReference Include="AsyncSemaphore" />
paket add AsyncSemaphore --version 2.0.0
#r "nuget: AsyncSemaphore, 2.0.0"
#:package AsyncSemaphore@2.0.0
#addin nuget:?package=AsyncSemaphore&version=2.0.0
#tool nuget:?package=AsyncSemaphore&version=2.0.0
AsyncSemaphore
A fast, allocation-free async semaphore featuring:
- Automatic releasing without try/finally blocks by utilising the IDisposable
usingpattern - Guarantee that release can only be called once per
WaitAsynccall - A custom lock-free core that outperforms
SemaphoreSlim(~2x faster uncontended, ~27% faster async handoff) and allocates zero bytes even for contended async waits (pooledIValueTaskSourcewaiters) - Analyzers to help you implement the desired pattern
- An
IAsyncSemaphoreinterface for if you need to mock
Install
dotnet add package AsyncSemaphore
Usage
private readonly AsyncSemaphore _asyncSemaphore = new AsyncSemaphore(1);
public async Task MyMethod()
{
// Just assign the `IDisposable` returned from `WaitAsync` to a variable and use the using statement with it
using var lockHandle = await _asyncSemaphore.WaitAsync();
// Do whatever you want - Even if we throw exceptions, we'll release the semaphore once we leave this method's scope
await DoSomethingInsideLock();
}
or scoped:
private readonly AsyncSemaphore _asyncSemaphore = new AsyncSemaphore(1);
public async Task MyMethod()
{
// or create your own scope with {} braces - And after you leave that scope, your lock will be released
using (await _asyncSemaphore.WaitAsync())
{
await DoSomethingInsideLock();
}
await DoSomethingAfterLockReleased();
}
Performance
AsyncSemaphore no longer wraps SemaphoreSlim — it has its own lock-free core:
- Uncontended waits are a single interlocked operation (no monitor lock): ~2x faster than
SemaphoreSlim. - Contended async waits use pooled (including thread-local cached)
IValueTaskSourcewaiter nodes: ~27% faster handoff and zero bytes allocated per wait, versus 88 B per async waiter forSemaphoreSlim.
BenchmarkDotNet v0.15.8, Windows 11
12th Gen Intel Core i7-12700K 3.60GHz, 1 CPU, 20 logical and 12 physical cores
.NET SDK 10.0.400
[Host] : .NET 10.0.11, X64 RyuJIT x86-64-v3
DefaultJob : .NET 10.0.11, X64 RyuJIT x86-64-v3
| Method | Categories | Mean | Ratio | Allocated | Alloc Ratio |
|---|---|---|---|---|---|
| SemaphoreSlim | Uncontended | 29.73 ns | 1.00 | - | NA |
| AsyncSemaphore | Uncontended | 15.16 ns | 0.51 | - | NA |
| SemaphoreSlim_AsyncHandoff | AsyncHandoff | 47.60 ns | 1.00 | 88 B | 1.00 |
| AsyncSemaphore_AsyncHandoff | AsyncHandoff | 34.98 ns | 0.73 | - | 0.00 |
| SemaphoreSlim_Parallel | Parallel | 710.74 ns | 1.00 | 89 B | 1.00 |
| AsyncSemaphore_Parallel | Parallel | 747.84 ns | 1.05 | - | 0.01 |
Uncontended is a wait/release cycle with the permit available. AsyncHandoff measures handing the permit to a queued async waiter. Parallel is 4 workers hammering a single-permit semaphore with an await Task.Yield() inside the lock — AsyncSemaphore trades ~5% mean time there for fully allocation-free waits.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- AsyncSemaphore.Analyzers (>= 2.0.0)
- Microsoft.Bcl.AsyncInterfaces (>= 9.0.8)
- System.Threading.Tasks.Extensions (>= 4.6.3)
-
net10.0
- AsyncSemaphore.Analyzers (>= 2.0.0)
-
net8.0
- AsyncSemaphore.Analyzers (>= 2.0.0)
-
net9.0
- AsyncSemaphore.Analyzers (>= 2.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on AsyncSemaphore:
| Package | Downloads |
|---|---|
|
ModularPipelines
Write your pipelines in C#! |
|
|
DotnetModularPipelines
Write your pipelines in C#! |
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on AsyncSemaphore:
| Repository | Stars |
|---|---|
|
thomhurst/ModularPipelines
Write your pipelines in C# !
|
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.0 | 63 | 9/1/2026 |
| 1.5.0 | 123,480 | 2/7/2026 |
| 1.4.6 | 145 | 2/7/2026 |
| 1.4.0 | 142 | 2/7/2026 |
| 1.3.273 | 130 | 2/7/2026 |
| 1.3.272 | 149 | 2/7/2026 |
| 1.3.0 | 87,090 | 12/1/2024 |
| 1.2.2 | 67,899 | 6/19/2024 |
| 1.2.1 | 233 | 6/19/2024 |
| 1.2.0 | 311 | 6/18/2024 |
| 1.1.0 | 236 | 6/18/2024 |
| 1.0.8 | 243 | 6/17/2024 |
| 1.0.6 | 214 | 6/17/2024 |
| 1.0.4 | 292 | 6/16/2024 |
| 1.0.3 | 324 | 6/16/2024 |
| 1.0.2 | 231 | 6/16/2024 |
| 1.0.0 | 251 | 6/16/2024 |