An open API service indexing awesome lists of open source software.

https://github.com/doug144/keyedlocks

A NuGet library for c# locking by a given key
https://github.com/doug144/keyedlocks

async csharp csharp-library lock locking netstandard netstandard20 nuget semaphoreslim

Last synced: 2 months ago
JSON representation

A NuGet library for c# locking by a given key

Awesome Lists containing this project

README

        

# KeyedLocks
Allows using [SemaphoreSlim](https://docs.microsoft.com/en-us/dotnet/api/system.threading.semaphoreslim?view=netstandard-2.0)
locks by a given key of any type.

[![Build Status](https://dev.azure.com/douglasgross/KeyedLock/_apis/build/status/doug144.KeyedLocks?branchName=master)](https://dev.azure.com/douglasgross/KeyedLock/_build/latest?definitionId=1&branchName=master)

## Usage

You can use the [KeyedLock NuGet](https://www.nuget.org/packages/KeyedLock/) as shown below.

[![NuGet](https://img.shields.io/nuget/v/KeyedLock.svg?style=flat-square&label=nuget)](https://www.nuget.org/packages/KeyedLock/)

````csharp
public class Foo
{
private KeyedLocker _locker = new KeyedLocker();

public void DoSomethingWithLock(string keyToLock)
{
_locker[keyToLock].Wait();
try
{
//do something...
}
finally
{
_locker[keyToLock].Release();
}
}
}
````

If your code will contain many keys, consider using the [HashedKeyedLocker](/KeyedLock/HashedKeyedLocker.cs) with a hash function to minimize the nubmer of
[SemaphoreSlim](https://docs.microsoft.com/en-us/dotnet/api/system.threading.semaphoreslim?view=netstandard-2.0)
objects that will be held in memory.

````csharp
public class Foo
{
private HashedKeyedLock _locker = new HashedKeyedLocker(long x => (int)(x % 10)); // limit to 10 locks in memory

public void DoSomethingWithLock(long keyToLock)
{
_locker[keyToLock].Wait();
try
{
//do something...
}
finally
{
_locker[keyToLock].Release();
}
}
}
````
## Resources
Some of the ideas for this repo were taken from [this blog post](https://www.tabsoverspaces.com/233703-named-locks-using-monitor-in-net-implementation).