CodeArt.ThreadUtils 2.1.1

dotnet add package CodeArt.ThreadUtils --version 2.1.1
                    
NuGet\Install-Package CodeArt.ThreadUtils -Version 2.1.1
                    
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="CodeArt.ThreadUtils" Version="2.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CodeArt.ThreadUtils" Version="2.1.1" />
                    
Directory.Packages.props
<PackageReference Include="CodeArt.ThreadUtils" />
                    
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 CodeArt.ThreadUtils --version 2.1.1
                    
#r "nuget: CodeArt.ThreadUtils, 2.1.1"
                    
#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 CodeArt.ThreadUtils@2.1.1
                    
#: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=CodeArt.ThreadUtils&version=2.1.1
                    
Install as a Cake Addin
#tool nuget:?package=CodeArt.ThreadUtils&version=2.1.1
                    
Install as a Cake Tool

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 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.
  • 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.

Version Downloads Last Updated
2.1.1 121 12/12/2025
2.1.0 115 12/12/2025
2.0.0 180 7/2/2025