Skip to content

roel-de-regt/keyed-semaphores

Β 
Β 

Repository files navigation

πŸ”‘πŸ”’ Keyed Semaphores

Build Status Nuget (with prereleases)

In multithreaded C#, it can be wasteful to use one single lock for all threads. Some examples:

  • When creating a lot of directories, you can create one lock for threads that create directories, or you could create a lock object per "highest level" directory
  • When processing a lot of bank transactions, you can process transactions in parallel, except when they come from the same person. In that case you probably want to run those transactions sequentially. In that case, you would create a keyed semaphore where the key is the ID of that person.
  • etc.

This library helps you create a lock object per key, and then use that lock object to improve the parallelism in your application.

Sample

Old version: no transactions run in parallel, because they might come from the same person, in which case the transactions must be processed sequentially

public class BankTransactionProcessor 
{
  private readonly object _lock = new object();
  
  public async Task Process(BankTransaction transaction) 
  {
    lock(_lock) 
    {
       ...
    }
  }
}

New version: all transactions can run in parallel, except the ones with the same person ID

public class BankTransactionProcessor
{
  public async Task Process(BankTransaction transaction) 
  {
    var key = transaction.Person.Id.ToString();
    using (await KeyedSemaphore.LockAsync(key))
    {
      ...
    }
  }
}

See working code sample at https://github.com/amoerie/keyed-semaphores/blob/master/KeyedSemaphores.Samples/Program.cs

Changelog

See the CHANGELOG.MD file

Contributors

See the CONTRIBUTORS.MD file

About

πŸ”‘πŸ”’ Lock your c# threads by key

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 98.3%
  • PowerShell 1.7%