CryptoHives.Foundation.Threading
0.5.34-preview
dotnet add package CryptoHives.Foundation.Threading --version 0.5.34-preview
NuGet\Install-Package CryptoHives.Foundation.Threading -Version 0.5.34-preview
<PackageReference Include="CryptoHives.Foundation.Threading" Version="0.5.34-preview" />
<PackageVersion Include="CryptoHives.Foundation.Threading" Version="0.5.34-preview" />
<PackageReference Include="CryptoHives.Foundation.Threading" />
paket add CryptoHives.Foundation.Threading --version 0.5.34-preview
#r "nuget: CryptoHives.Foundation.Threading, 0.5.34-preview"
#:package CryptoHives.Foundation.Threading@0.5.34-preview
#addin nuget:?package=CryptoHives.Foundation.Threading&version=0.5.34-preview&prerelease
#tool nuget:?package=CryptoHives.Foundation.Threading&version=0.5.34-preview&prerelease
🛡️ CryptoHives Open Source Initiative 🐝
An open, community-driven cryptography and performance library collection for the .NET ecosystem, developed and maintained by The Keepers of the CryptoHives.
CryptoHives.Foundation.Threading
High-performance, pooled async synchronization primitives for .NET — designed to eliminate Task and TaskCompletionSource<T> allocations on the hot path.
All primitives are backed by ObjectPool<T> and return ValueTask<T> to minimize per-operation allocations in high-throughput scenarios.
Included: This package automatically bundles CryptoHives.Foundation.Threading.Analyzers — Roslyn analyzers that detect common
ValueTaskmisuse patterns at compile time, applied transitively to all consumers.
📦 Installation
dotnet add package CryptoHives.Foundation.Threading
✨ Key Features
- Pooled primitives — synchronization objects backed by
Microsoft.Extensions.ObjectPool ValueTask-based APIs — avoidTaskallocations when operations complete synchronouslyCancellationTokensupport — full cancellation across all primitives, allocation-free on modern .NETConfigureAwaitsupport — works naturally with.ConfigureAwait(false)in library code- Configurable continuations — control synchronous vs. asynchronous continuation scheduling
- Custom pools — supply your own
IGetPooledManualResetValueTaskSource<T>for fine-grained control - Drop-in replacement — change namespace, keep the same
using-based patterns - Included analyzers — ValueTask misuse caught at compile time
🚀 Quick Examples
Mutual Exclusion — AsyncLock
using CryptoHives.Foundation.Threading.Async.Pooled;
private readonly AsyncLock _lock = new();
public async Task DoWorkAsync(CancellationToken ct)
{
using (await _lock.LockAsync(ct).ConfigureAwait(false))
{
// Critical section — only one task at a time
await ModifySharedStateAsync().ConfigureAwait(false);
}
}
Producer-Consumer — AsyncAutoResetEvent
private readonly AsyncAutoResetEvent _itemReady = new(initialState: false);
private readonly Queue<Item> _queue = new();
// Producer
public void Enqueue(Item item)
{
_queue.Enqueue(item);
_itemReady.Set(); // Releases exactly one waiter
}
// Consumer
public async Task<Item> DequeueAsync(CancellationToken ct)
{
await _itemReady.WaitAsync(ct).ConfigureAwait(false);
return _queue.Dequeue();
}
Broadcast — AsyncManualResetEvent
private readonly AsyncManualResetEvent _ready = new(initialState: false);
public async Task InitializeAsync()
{
await LoadConfigurationAsync().ConfigureAwait(false);
_ready.Set(); // All waiters are released at once
}
public async Task UseServiceAsync(CancellationToken ct)
{
await _ready.WaitAsync(ct).ConfigureAwait(false);
// Service is initialized
}
Bounded Concurrency — AsyncSemaphore
private readonly AsyncSemaphore _semaphore = new(initialCount: 4);
public async Task FetchAsync(CancellationToken ct)
{
await _semaphore.WaitAsync(ct).ConfigureAwait(false);
try
{
await CallExternalServiceAsync().ConfigureAwait(false);
}
finally
{
_semaphore.Release();
}
}
Read-Write Separation — AsyncReaderWriterLock
private readonly AsyncReaderWriterLock _rwLock = new();
public async Task<Data> ReadAsync(CancellationToken ct)
{
using (await _rwLock.ReaderLockAsync(ct).ConfigureAwait(false))
return _cache.Get();
}
public async Task WriteAsync(Data data, CancellationToken ct)
{
using (await _rwLock.WriterLockAsync(ct).ConfigureAwait(false))
_cache.Set(data);
}
Custom Pool
using CryptoHives.Foundation.Threading.Pools;
var policy = new PooledValueTaskSourceObjectPolicy<bool>();
var pool = new ValueTaskSourceObjectPool<bool>(policy, maximumRetained: 64);
var evt = new AsyncAutoResetEvent(
initialState: false,
runContinuationAsynchronously: true,
pool: pool);
⚠️ ValueTask Contract
- Await a
ValueTaskexactly once — multipleawaitorAsTask()calls may throwInvalidOperationException. - Avoid
AsTask()before signaling — whenRunContinuationsAsynchronously=true(the default), storing the result ofAsTask()before the primitive is signaled causes severe performance degradation. Await theValueTaskdirectly wherever possible. - Always await or discard — if a waiter is not consumed, the underlying
IValueTaskSourceis not returned to the pool.
The included Threading.Analyzers enforce these rules automatically at compile time.
📚 Documentation
| Resource | Link |
|---|---|
| Full package documentation | cryptohives.github.io/Foundation/packages/threading |
| API reference | cryptohives.github.io/Foundation/api |
| Benchmarks | cryptohives.github.io/Foundation/packages/threading/benchmarks |
| Threading.Analyzers | cryptohives.github.io/Foundation/packages/threading.analyzers |
| Source repository | github.com/CryptoHives/Foundation |
🔐 Security Policy
If you discover a vulnerability, please do not open a public issue. Follow the guidelines on the CryptoHives Security Page.
⚖️ License
MIT — © 2026 The Keepers of the CryptoHives
| 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 was computed. 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 is compatible. |
| .NET Framework | net461 was computed. net462 is compatible. 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. |
-
.NETFramework 4.6.2
- Microsoft.Bcl.AsyncInterfaces (>= 10.0.8)
- Microsoft.Bcl.HashCode (>= 6.0.0)
- Microsoft.Extensions.ObjectPool (>= 10.0.8)
- System.Memory (>= 4.6.3)
- System.Threading.Tasks.Extensions (>= 4.6.3)
-
.NETStandard 2.0
- Microsoft.Bcl.AsyncInterfaces (>= 10.0.8)
- Microsoft.Bcl.HashCode (>= 6.0.0)
- Microsoft.Extensions.ObjectPool (>= 10.0.8)
- System.Memory (>= 4.6.3)
- System.Threading.Tasks.Extensions (>= 4.6.3)
-
.NETStandard 2.1
- Microsoft.Extensions.ObjectPool (>= 10.0.8)
-
net10.0
- Microsoft.Extensions.ObjectPool (>= 10.0.8)
-
net8.0
- Microsoft.Extensions.ObjectPool (>= 10.0.8)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.5.34-preview | 0 | 6/2/2026 |
| 0.5.21-preview | 107 | 5/2/2026 |
| 0.5.13-preview | 62 | 4/2/2026 |
| 0.4.21-preview | 71 | 3/1/2026 |
| 0.4.11-preview | 77 | 2/14/2026 |
| 0.3.19-preview | 77 | 1/26/2026 |
| 0.2.43-preview | 79 | 1/9/2026 |
| 0.2.33-preview | 425 | 12/9/2025 |
| 0.2.30-preview | 339 | 12/8/2025 |
| 0.2.28-preview | 272 | 12/7/2025 |
| 0.2.26-preview | 214 | 12/6/2025 |
| 0.2.22-preview | 562 | 12/1/2025 |
| 0.2.17-preview | 174 | 11/23/2025 |
| 0.2.13-preview | 397 | 11/20/2025 |
| 0.2.11-preview | 989 | 11/19/2025 |