CodeArt.ThreadUtils
2.1.1
dotnet add package CodeArt.ThreadUtils --version 2.1.1
NuGet\Install-Package CodeArt.ThreadUtils -Version 2.1.1
<PackageReference Include="CodeArt.ThreadUtils" Version="2.1.1" />
<PackageVersion Include="CodeArt.ThreadUtils" Version="2.1.1" />
<PackageReference Include="CodeArt.ThreadUtils" />
paket add CodeArt.ThreadUtils --version 2.1.1
#r "nuget: CodeArt.ThreadUtils, 2.1.1"
#:package CodeArt.ThreadUtils@2.1.1
#addin nuget:?package=CodeArt.ThreadUtils&version=2.1.1
#tool nuget:?package=CodeArt.ThreadUtils&version=2.1.1
CodeArt.ThreadUtils
A .NET library providing thread coordination primitives that work seamlessly with C# async/await pattern. Designed for high-performance concurrent applications where proper thread synchronization is critical. The lock primitives are based on and inspired by the series of blog posts by Stephen Toub on the dotnet Dev Blogs.
The implementation is a bit different from the one in the blog because I want to have the log primitives support both Asynchronous and Synchronous callers.
The lock primitives are not complete (for now). I plan to add AsyncSemaphore, AsyncManualResetEvent, etc.
Features
AsyncLock
A lock that supports both synchronous and asynchronous acquisition. This eliminates the need to block threads when waiting for a lock in async contexts.
private readonly AsyncLock _lock = new();
// Async usage
async Task DoWorkAsync()
{
using (await _lock.LockAsync())
{
// Critical section (async)
}
}
// Sync usage
void DoWork()
{
using (_lock.Lock())
{
// Critical section (sync)
}
}
AsyncReaderWriterLock
A reader-writer lock supporting both synchronous and asynchronous acquisition. Allows multiple concurrent readers or a single writer.
private readonly AsyncReaderWriterLock _rwLock = new();
// Reader (async)
async Task ReadDataAsync()
{
using (await _rwLock.ReaderLockAsync())
{
// Read operations
}
}
// Writer (async)
async Task WriteDataAsync()
{
using (await _rwLock.WriterLockAsync())
{
// Write operations
}
}
KeyedAsyncLock
Locks based on keys (e.g., resource IDs) without the overhead of maintaining lock objects for every possible key. Only allocates resources when locks are actively being used.
private readonly KeyedAsyncLock<string> _keyedLock = new();
async Task ProcessResourceAsync(string resourceId)
{
using (await _keyedLock.LockAsync(resourceId))
{
// Process the specific resource
}
}
InterlockedEx
Extensions to the standard Interlocked class that allow applying arbitrary functions atomically to numbers.
private int _counter;
// Atomically apply a function to the counter
int newValue = InterlockedEx.Apply(ref _counter, x => x * 2 + 1);
Installation
Install via NuGet:
Install-Package CodeArt.ThreadUtils
Requirements
- .NET 9.0 or higher
License
MIT License
| 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
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.