ShardedCounter 6.0.2
dotnet add package ShardedCounter --version 6.0.2
NuGet\Install-Package ShardedCounter -Version 6.0.2
<PackageReference Include="ShardedCounter" Version="6.0.2" />
<PackageVersion Include="ShardedCounter" Version="6.0.2" />
<PackageReference Include="ShardedCounter" />
paket add ShardedCounter --version 6.0.2
#r "nuget: ShardedCounter, 6.0.2"
#:package ShardedCounter@6.0.2
#addin nuget:?package=ShardedCounter&version=6.0.2
#tool nuget:?package=ShardedCounter&version=6.0.2

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
Interlockedvalue 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.0net8.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
ShardedCounteragainst a plainInterlockedcounter 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
- CI runs on pushes and pull requests through
.github/workflows/ci.yml. - NuGet publishing is gated on version tags through
.github/workflows/release.yml. - Create a tag like
v6.0.2after updating package metadata and CHANGELOG.md to publish the next release.
| Product | Versions 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. |
-
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.
Modernized the API and packaging, added net8.0 support, expanded concurrent validation, and introduced CI/release automation.