ShardedCounter 6.0.2

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

logo

NuGet Status MIT License CI Release

Simplistic, atomic, interlocked counter that spreads writes across per-thread shards so concurrent updates do not all contend on a single shared value.

Why this exists

ShardedCounter is optimized for write-heavy concurrent scenarios. Each thread writes to its own shard, and reads compute the current total by summing the shard values.

This tradeoff is useful when:

  • many threads are updating the counter frequently
  • reads are less frequent than writes
  • a single Interlocked value becomes a hotspot under contention

Installation

dotnet add package ShardedCounter

Usage

using ShardedCounter.Core;

var counter = new ShardedCounter();

counter.Add(5);
counter.Add(-2);
counter.Increment();
counter.Decrement();

Console.WriteLine(counter.Count); // 3

Public API

public class ShardedCounter
{
    public void Add(long amount);
    public void Increase(long amount);
    public void Decrease(long amount);
    public void Increment();
    public void Decrement();
    public long Count { get; }
}

Add(long amount) is the recommended API because it makes signed counter updates explicit. Increase and Decrease remain available as convenience and compatibility methods.

Target frameworks

The library currently targets:

  • net6.0
  • net8.0

Notes

  • Writes are cheap because they stay on a thread-local shard.
  • Reads are more expensive because they sum all known shards.
  • This library is best suited to counters that are updated often and read occasionally.

Validation

  • Unit tests cover signed updates, compatibility APIs, and concurrent writer scenarios.
  • The benchmark project compares ShardedCounter against a plain Interlocked counter under multiple thread counts.

Run the benchmarks with:

dotnet run -c Release --project ShardedCounter.Core.Benchmarks

BenchmarkDotNet will emit markdown and other result artifacts under BenchmarkDotNet.Artifacts/. If you want benchmark snapshots in the repo, rerun the benchmark project intentionally and check in the generated markdown summary you want to preserve.

Releases

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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.
  • net6.0

    • No dependencies.
  • net8.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
6.0.2 91 4/1/2026
6.0.1 1,088 10/16/2022
6.0.0 1,049 10/13/2022

Modernized the API and packaging, added net8.0 support, expanded concurrent validation, and introduced CI/release automation.