AsyncKeyedLock 3.1.1

AsyncKeyedLock AsyncKeyedLock

GitHub Workflow Status Nuget Nuget

An asynchronous .NET Standard 2.0 library that allows you to lock based on a key (keyed semaphores), only allowing a defined number of concurrent threads that share the same key.

For example, if you're processing transactions, you may want to limit to only one transaction per user so that the order is maintained, but meanwhile allowing parallel processing of multiple users.

Benchmarks

Tests show that AsyncKeyedLock is considerably faster than similar libraries due to its neat locking mechanism.

Installation

The recommended means is to use NuGet, but you could also download the source code from here.

Usage

var asyncKeyedLocker = new AsyncKeyedLocker();
using (var lockObj = await asyncKeyedLocker.LockAsync(myObject))
{
	...
}

You can also set the maximum number of requests for the semaphore that can be granted concurrently (set to 1 by default):

var asyncKeyedLocker = new AsyncKeyedLocker(2);

If you would like to see how many concurrent requests there are for a semaphore for a given key:

int myRemainingCount = asyncKeyedLocker.GetRemainingCount(myObject);

If you would like to see the number of remaining threads that can enter the lock for a given key:

int myCurrentCount = asyncKeyedLocker.GetCurrentCount(myObject);

If you would like to check whether any request is using a specific key:

bool isInUse = asyncKeyedLocker.IsInUse(myObject);

And if for some reason you need to force release the requests in the semaphore for a key:

asyncKeyedLocker.ForceRelease(myObject);

You may also use Dependency Injection to inject an instance of AsyncKeyedLock.

Credits

This library is based on Stephen Cleary's solution.

Showing the top 20 packages that depend on AsyncKeyedLock.

Packages Downloads
Volo.Abp.DistributedLocking.Abstractions
Package Description
21
Volo.Abp.DistributedLocking.Abstractions
Package Description
20
Volo.Abp.DistributedLocking.Abstractions
Package Description
19
Volo.Abp.DistributedLocking.Abstractions
Package Description
18

ForceRelease now releases everything in the semaphore before removing the key from the dictionary.

.NET Standard 2.0

  • No dependencies.

Version Downloads Last updated
8.0.2 10 2/17/2026
8.0.1 9 1/24/2026
8.0.1-alpha 13 1/4/2026
8.0.0 19 1/4/2026
8.0.0-beta2 16 1/4/2026
8.0.0-beta 18 1/4/2026
7.1.8 20 12/11/2025
7.1.8-beta5 16 12/12/2025
7.1.8-beta4 17 12/12/2025
7.1.8-beta3 15 12/12/2025
7.1.8-beta2 16 12/12/2025
7.1.8-beta 14 12/12/2025
7.1.8-alpha 15 1/4/2026
7.1.7 16 12/12/2025
7.1.6 17 12/12/2025
7.1.5 14 1/4/2026
7.1.5-alpha 14 12/12/2025
7.1.4 12 12/12/2025
7.1.4-preview 18 12/12/2025
7.1.3 18 12/12/2025
7.1.2 14 1/4/2026
7.1.1 14 1/4/2026
7.1.0 13 1/4/2026
7.1.0-alpha 15 1/4/2026
7.0.2 17 12/12/2025
7.0.1 16 12/12/2025
7.0.1-alpha 13 1/4/2026
7.0.0 15 12/12/2025
7.0.0-rc3 16 12/12/2025
7.0.0-rc2 15 12/12/2025
7.0.0-rc1 15 12/12/2025
7.0.0-beta 15 12/12/2025
7.0.0-alpha 16 12/12/2025
6.4.2 16 12/12/2025
6.4.1 15 12/12/2025
6.4.0 19 12/12/2025
6.3.4 16 12/12/2025
6.3.4-rc 15 12/12/2025
6.3.4-beta 21 12/12/2025
6.3.4-alpha 13 1/4/2026
6.3.3 16 12/12/2025
6.3.2 16 12/12/2025
6.3.1 12 1/4/2026
6.3.0 15 12/12/2025
6.2.7-alpha 14 1/4/2026
6.2.6 24 12/12/2025
6.2.6-alpha 14 1/4/2026
6.2.5 24 12/12/2025
6.2.5-alpha 12 1/4/2026
6.2.4 22 12/12/2025
6.2.3 21 12/12/2025
6.2.3-beta 17 12/12/2025
6.2.2 19 12/12/2025
6.2.2-beta 15 1/4/2026
6.2.2-alpha 13 1/4/2026
6.2.1 15 12/12/2025
6.2.0 17 12/12/2025
6.2.0-alpha 12 1/4/2026
6.1.2-rc 14 1/4/2026
6.1.2-beta 16 1/4/2026
6.1.2-alpha 14 1/4/2026
6.1.1 17 12/12/2025
6.1.1-rc 18 12/12/2025
6.1.1-beta 12 12/12/2025
6.1.0 21 12/12/2025
6.0.5 23 12/12/2025
6.0.5-alpha 18 12/12/2025
6.0.4 18 12/12/2025
6.0.4-rc6 16 12/12/2025
6.0.4-rc5 13 12/12/2025
6.0.4-rc3 17 12/12/2025
6.0.4-rc2 13 1/4/2026
6.0.4-rc 18 12/11/2025
6.0.4-beta 19 12/12/2025
6.0.4-alpha 13 12/12/2025
6.0.3 20 12/12/2025
6.0.2 22 12/12/2025
6.0.1 15 12/12/2025
6.0.0 18 1/4/2026
5.1.3-beta 14 1/4/2026
5.1.3-alpha 13 1/4/2026
5.1.2 19 12/12/2025
5.1.1 20 12/12/2025
5.1.1-rc 11 1/4/2026
5.1.1-alpha 13 1/4/2026
5.1.0 18 12/12/2025
5.1.0-alpha 11 1/4/2026
5.0.6-alpha 14 1/4/2026
5.0.5-rc 13 1/4/2026
5.0.5-alpha 12 1/4/2026
5.0.4 15 12/12/2025
5.0.3 22 12/12/2025
5.0.3-rc 15 12/12/2025
5.0.2-rc 14 12/12/2025
5.0.1 19 12/12/2025
5.0.0 13 1/4/2026
4.0.2 18 12/12/2025
4.0.1 17 1/4/2026
4.0.0 13 1/4/2026
3.2.3 18 12/12/2025
3.2.1 21 12/12/2025
3.2.0 18 12/12/2025
3.1.1 14 1/4/2026
3.1.0 13 1/4/2026
3.0.1 15 12/12/2025
3.0.0 17 12/12/2025
2.0.3 19 12/12/2025
2.0.2 18 12/12/2025
2.0.1 14 12/12/2025
2.0.0 22 12/12/2025
1.1.0 20 12/12/2025
1.0.1 18 12/12/2025
1.0.0 16 12/12/2025