Singulink.Threading 2.1.0

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

Singulink.Threading

Chat on Discord View nuget packages Build

Singulink.Threading is a small utility library used to support other Singulink projects with some common multi-threading related functionality. It has a key-based asynchronous-capable locking mechanism, common interlocked spin operation helpers, reader/writer lock extensions and an interlocked flag implementation.

We are a small team of engineers and designers dedicated to building beautiful, functional, and well-engineered software solutions. We offer very competitive rates as well as fixed-price contracts and welcome inquiries to discuss any custom development / project support needs you may have.

This package is part of our Singulink Libraries collection. Visit https://github.com/Singulink to see our full list of publicly available libraries and other open-source projects.

Installation

The package is available on NuGet - simply install the Singulink.Threading package.

Supported Runtimes: .NET 8.0+

API

You can view the fully documented API on the project documentation site.

Usage

This library makes use of mutable structs as a low-level performance optimization. These structs have been annotated with a [NonCopyable] attribute to allow a non-copyable struct analyzer to detect misuse. When using this library, it is recommend that either NonCopyableAnalyzer or Roslyn.Diagnostics.Analyzers is added to the project to warn on potentially unintended copying of these structs.

InterlockedFlag

public class ExecuteOnce
{
    private InterlockedFlag _executedFlag;

    public bool DidExecute => _executedFlag.IsSet;

    public void Execute()
    {
        if (_executedFlag.TrySet())
        {
            // Run code that should only execute once
        }
    }

    public void AllowOneMoreRun()
    {
        _executedFlag.TryClear();
    }
}

InterlockedSpin

const int MaxClients = 10;

int _clientCount;

void OnClientConnect()
{
    if (!InterlockedSpin.TryIncrementToMax(ref _clientCount, MaxClients))
        RefuseConnection();
}

void OnClientDisconnect()
{
    Interlocked.Decrement(ref _clientCount);
}
int[] _items = [1, 2, 3];

// Returns [1, 2, 3, 4] on the first call, [1, 2, 3, 4, 5] on the second call, etc.
int[] AddNextItem()
{
    return InterlockedSpin.Exchange(ref _items, items => [..items, items[^1] + 1]);
}

KeyLocker

KeyLocker<string> _locker = new(StringComparer.IgnoreCase);

void ProcessItem(string itemId)
{
    using (_locker.Lock(itemId))
    {
        // Safe to process the item here without concurrent access
        DoProcessing(itemId);
    }
}

async Task ProcessItemAsync(string itemId)
{
    using (await _locker.LockAsync(itemId))
    {
        // Safe to process the item here without concurrent access
        DoProcessing(itemId);
    }
}

ReaderWriterLockSlimExtensions

When you import the Singulink.Threading namespace, 3 new extension methods appear on ReaderWriterLockSlim instances:

  • EnterReadGuard()
  • EnterWriteGuard()
  • EnterUpgradeableReadGuard()
using Singulink.Threading;

ReaderWriterLockSlim lock = new();

// Locks are acquired inside the using blocks and released at the end:

using (lock.EnterReadGuard())
{
    // Safe to read here
    ReadData();
}

using (lock.EnterWriteGuard())
{
    // Safe to write here
    WriteData();
}

using (var upgradeableGuard = lock.EnterUpgradeableReadGuard())
{
    // Safe to read here
    ReadData();

    if (someCondition)
    {
        upgradeableGuard.UpgradeToWriteGuard();
        // Safe to write here
        WriteData();
    }
}
Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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.
  • net8.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Singulink.Threading:

Package Downloads
Singulink.FulcrumFS

Transactional file processing pipeline and storage engine with two-phase commit and file variant management.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.0 20 7/27/2025
2.0.2 89 7/26/2025
2.0.1 91 7/26/2025
2.0.0 583 7/24/2025 2.0.0 is deprecated because it has critical bugs.
1.0.0 675 10/24/2020