Soenneker.Atomics.ValueInts 4.0.26

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

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

Soenneker.Atomics.ValueInts

A lightweight, allocation-free atomic int struct backed by Volatile and Interlocked operations. Intended for use as a private field / inline synchronization primitive. Because this is a mutable struct, avoid copying it (e.g., returning it from properties or storing it in collections where it may be copied by value).

Install

dotnet add package Soenneker.Atomics.ValueInts

Usage

Use ValueAtomicInt as a field, not as a copied local value:

using Soenneker.Atomics.ValueInts;

public sealed class QueueMetrics
{
    private ValueAtomicInt _depth;

    public int Enqueued() => _depth.Increment();
    public int Dequeued() => _depth.Decrement();
    public int ReadDepth() => _depth.Read();
}

Because this is a mutable struct, returning it from a property or passing it by value creates an independent counter. Use the reference-type AtomicInt package when multiple objects must share the wrapper itself.

Update and Accumulate may invoke their delegate repeatedly during compare-and-exchange retries. Delegates must be side-effect free.

What you get

  • ValueAtomicInt — A lightweight, allocation-free atomic int struct backed by Volatile and Interlocked operations. Intended for use as a private field / inline synchronization primitive. Because this is a mutable struct, avoid copying it (e.g., returning it from properties or storing it in collections where it may be copied by value).

API at a glance

API What it does Result / important behavior
ValueAtomicInt.Value Gets or sets the current value. Gets or sets the current value.
ValueAtomicInt.Read() Reads the current value using acquire semantics. The current value observed with acquire memory-ordering semantics.
ValueAtomicInt.Exchange(value) Atomically replaces the current value with value and returns the previous value. The value that was stored before the atomic update.
ValueAtomicInt.CompareExchange(value, comparand) Atomically sets the value to value if the current value equals comparand. Returns the original value. The value observed before the compare-and-exchange attempt.
ValueAtomicInt.TryCompareExchange(value, comparand) Attempts to set the value to value if the current value equals comparand. true if the requested update was applied; otherwise, false.
ValueAtomicInt.Increment() Atomically increments the value and returns the incremented value. The incremented value.
ValueAtomicInt.Decrement() Atomically decrements the value and returns the decremented value. The decremented value.
ValueAtomicInt.Add(delta) Atomically adds delta and returns the resulting value. The resulting value.
ValueAtomicInt.Or(mask) Atomically applies a bitwise OR mask. The value observed before the OR operation. Read again when the resulting value is required.
ValueAtomicInt.And(mask) Atomically applies a bitwise AND mask. The value observed before the AND operation. Read again when the resulting value is required.
ValueAtomicInt.GetAndIncrement() Atomically increments the value and returns the previous value. The value that was stored before the atomic update.
ValueAtomicInt.GetAndDecrement() Atomically decrements the value and returns the previous value. The value that was stored before the atomic update.
ValueAtomicInt.GetAndAdd(delta) Atomically adds delta and returns the previous value. The value that was stored before the atomic update.
ValueAtomicInt.AddAndGet(delta) Atomically adds delta and returns the resulting value. The resulting value.
ValueAtomicInt.IncrementAndGet() Atomically increments the value and returns the resulting value. The resulting value.
ValueAtomicInt.DecrementAndGet() Atomically decrements the value and returns the resulting value. The resulting value.
ValueAtomicInt.TrySetIfGreater(value) Attempts to set the value to value if it is greater than the current value. true if the requested update was applied; otherwise, false.
ValueAtomicInt.TrySetIfLess(value) Attempts to set the value to value if it is less than the current value. true if the requested update was applied; otherwise, false.

Important behavior

  • ValueAtomicInt.Or(mask): This method returns the value from before the atomic update, matching Interlocked.Or semantics.
  • ValueAtomicInt.And(mask): This method returns the value from before the atomic update, matching Interlocked.And semantics.
  • ValueAtomicInt.VolatileWrite(value): Use this method to update the field in multithreaded scenarios where it is important that the most recent value is observed by all threads. This method provides a memory barrier to prevent certain types of reordering by the compiler or processor.
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 (7)

Showing the top 5 NuGet packages that depend on Soenneker.Atomics.ValueInts:

Package Downloads
Soenneker.Utils.BackgroundQueue

A high-performance background Task/ValueTask queue

Soenneker.Atomics.ValueBools

A thread-safe bool struct implemented on top of Interlocked/Volatile operations.

Soenneker.Asyncs.Locks

The fastest .NET async lock.

Soenneker.Atomics.ValueNullableBools

A lightweight, allocation-free atomic nullable boolean struct implemented on top of an inline ValueAtomicInt

Soenneker.Atomics.NullableBools

A lock free atomic nullable boolean.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.26 59,312 8/29/2026
4.0.25 60 8/29/2026
4.0.24 58 8/29/2026
4.0.23 406,765 7/27/2026
4.0.22 191,909 7/16/2026
4.0.21 260,926 6/19/2026
4.0.20 252,497 6/5/2026
4.0.19 121 6/5/2026
4.0.18 327,544 4/22/2026
4.0.16 285,126 3/12/2026
4.0.15 136 3/12/2026
4.0.14 139 3/12/2026
4.0.13 3,791 3/12/2026
4.0.12 89,794 3/10/2026
4.0.11 50,002 3/9/2026
4.0.10 145 3/9/2026
4.0.9 129 3/9/2026
4.0.8 107,559 3/4/2026
4.0.7 257,410 1/21/2026
4.0.6 132,920 1/6/2026
Loading failed

Improve README documentation