GithubHelp home page GithubHelp logo

camerondm9 / keyedlocks Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 20 KB

Sync and async locking based on a key

Home Page: https://www.nuget.org/packages/KeyedLocks

License: MIT License

C# 100.00%
synchronization

keyedlocks's Introduction

KeyedLocks

Sync and async locking based on a key

Usage

This library provides the KeyedLock<T> type, which can be used like this:

var locks = new KeyedLock<string>();
using (locks.Lock("key"))
{
    //Do the important thing
}

The using block ensures that the lock will be released after.

In async code, you can also wait to acquire the lock asynchronously:

var locks = new KeyedLock<string>();
using (await locks.LockAsync("key"))
{
    //Do the important thing
}

Testing

KeyedLock<T> is internally based on a Dictionary<T, SemaphoreSlim>. Semaphores are created and destroyed as needed.

This code has been tested with Microsoft Coyote to try to eliminate concurrency bugs.

Changelog

1.0.0

Initial release

keyedlocks's People

Contributors

camerondm9 avatar

Stargazers

 avatar  avatar

Watchers

 avatar

keyedlocks's Issues

Potential misuse

Directly returning the KeyedLock.Entry is efficient (no extra GC pressure), but allows for potential misuse if someone keeps a reference to it. They could call Dispose multiple times (or none).

We should probably return a wrapper class instead, so we can throw an exception if Dispose is called a second time.

Not sure what to do if they aren't calling Dispose at all, then their locks will stay locked forever and cause a memory leak...

Alternative: make all fields internal (on KeyedLock.Entry and add an analyzer that warns about putting it in a variable instead of a direct using statement.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.