Singulink.Threading
2.0.0
Prefix Reserved
AsyncLock with a timeout provided not throwing TimeoutException fixed in v2.0.1
See the version list below for details.
dotnet add package Singulink.Threading --version 2.0.0
NuGet\Install-Package Singulink.Threading -Version 2.0.0
<PackageReference Include="Singulink.Threading" Version="2.0.0" />
<PackageVersion Include="Singulink.Threading" Version="2.0.0" />
<PackageReference Include="Singulink.Threading" />
paket add Singulink.Threading --version 2.0.0
#r "nuget: Singulink.Threading, 2.0.0"
#:package Singulink.Threading@2.0.0
#addin nuget:?package=Singulink.Threading&version=2.0.0
#tool nuget:?package=Singulink.Threading&version=2.0.0
Singulink.Threading
Singulink.Threading is a small utility library for assisting with multithreading-related tasks. It has a key-based asynchronous-capable locking mechanism, common interlocked spin operation helpers, read/write lock extensions and an interlocked boolean flag implementation.
About Singulink
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 Examples
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 | Versions 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. |
-
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.