Soenneker.Asyncs.Semaphores
4.0.6
Prefix Reserved
dotnet add package Soenneker.Asyncs.Semaphores --version 4.0.6
NuGet\Install-Package Soenneker.Asyncs.Semaphores -Version 4.0.6
<PackageReference Include="Soenneker.Asyncs.Semaphores" Version="4.0.6" />
<PackageVersion Include="Soenneker.Asyncs.Semaphores" Version="4.0.6" />
<PackageReference Include="Soenneker.Asyncs.Semaphores" />
paket add Soenneker.Asyncs.Semaphores --version 4.0.6
#r "nuget: Soenneker.Asyncs.Semaphores, 4.0.6"
#:package Soenneker.Asyncs.Semaphores@4.0.6
#addin nuget:?package=Soenneker.Asyncs.Semaphores&version=4.0.6
#tool nuget:?package=Soenneker.Asyncs.Semaphores&version=4.0.6
Soenneker.Asyncs.Semaphores
A low-allocation asynchronous semaphore for bounded in-process concurrency.
The single-waiter path uses a direct handoff slot. Heavier contention spills into a zero-allocation intrusive MPSC queue backed by pooled IValueTaskSource waiters. Permit count and pending handoff ownership share one atomic state, allowing competing releasers to batch queue consumption with less synchronization.
Installation
dotnet add package Soenneker.Asyncs.Semaphores
Usage
Create a semaphore with the desired concurrency limit, then dispose every acquired lease:
using Soenneker.Asyncs.Semaphores;
var semaphore = new AsyncSemaphore(4);
using SemaphoreLease lease = await semaphore.Acquire(cancellationToken);
await ProcessAsync(cancellationToken);
The single-argument constructor makes the initial and maximum permit counts equal. To begin with fewer permits available, specify both values:
var semaphore = new AsyncSemaphore(initialCount: 0, maxCount: 4);
semaphore.Release();
Release() and Release(int) can signal permits explicitly. Disposing a lease releases its owned permit automatically, so do not manually release that same permit.
Try without waiting
if (semaphore.TryAcquire(out SemaphoreLease lease))
{
using (lease)
{
// One permit is owned here.
}
}
Acquire returns a ValueTask<SemaphoreLease> and the lease is a value type, so an immediately available permit requires no allocation. Contended waiter nodes are pooled for reuse. SemaphoreLease.Dispose() is idempotent for the same lease variable; because it is a mutable struct, do not dispose copies of a lease.
| Product | Versions 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. |
-
net10.0
- Soenneker.Queues.Intrusive.ValueMpsc (>= 4.0.36)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Soenneker.Asyncs.Semaphores:
| Package | Downloads |
|---|---|
|
Soenneker.Compression.SevenZip
A utility library for 7zip compression related operations |
|
|
Soenneker.Playwrights.Crawler
A configurable Playwright crawler with rich stealth and control options. |
|
|
Soenneker.Pdfs.Html
A utility library for generating PDF documents from HTML using Playwright. |
|
|
Soenneker.Flywheel.Dashboard
A real-time Blazor dashboard for monitoring and managing Soenneker Flywheel. |
GitHub repositories
This package is not used by any popular GitHub repositories.