{"id":22897144,"url":"https://github.com/doug144/keyedlocks","last_synced_at":"2025-04-01T00:43:46.721Z","repository":{"id":144136394,"uuid":"172381121","full_name":"doug144/KeyedLocks","owner":"doug144","description":"A NuGet library for c# locking by a given key","archived":false,"fork":false,"pushed_at":"2019-03-04T19:25:43.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-01T00:43:42.434Z","etag":null,"topics":["async","csharp","csharp-library","lock","locking","netstandard","netstandard20","nuget","semaphoreslim"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/doug144.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-24T19:35:33.000Z","updated_at":"2019-03-04T19:25:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"dc6ea2b4-3dc3-4d09-b8ac-e9684f039f7d","html_url":"https://github.com/doug144/KeyedLocks","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doug144%2FKeyedLocks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doug144%2FKeyedLocks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doug144%2FKeyedLocks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doug144%2FKeyedLocks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/doug144","download_url":"https://codeload.github.com/doug144/KeyedLocks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246563384,"owners_count":20797446,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","csharp","csharp-library","lock","locking","netstandard","netstandard20","nuget","semaphoreslim"],"created_at":"2024-12-14T00:15:33.895Z","updated_at":"2025-04-01T00:43:46.713Z","avatar_url":"https://github.com/doug144.png","language":"C#","readme":"# KeyedLocks\nAllows using [SemaphoreSlim](https://docs.microsoft.com/en-us/dotnet/api/system.threading.semaphoreslim?view=netstandard-2.0)\nlocks by a given key of any type.\n\n[![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\u0026branchName=master)\n\n## Usage\n\nYou can use the [KeyedLock NuGet](https://www.nuget.org/packages/KeyedLock/) as shown below.\n\n[![NuGet](https://img.shields.io/nuget/v/KeyedLock.svg?style=flat-square\u0026label=nuget)](https://www.nuget.org/packages/KeyedLock/) \n\n````csharp\npublic class Foo\n{\n    private KeyedLocker\u003cstring\u003e _locker = new KeyedLocker\u003cstring\u003e();\n  \n    public void DoSomethingWithLock(string keyToLock)\n    {\n        _locker[keyToLock].Wait();\n        try\n        {\n            //do something...\n        }\n        finally\n        {\n            _locker[keyToLock].Release();\n        }\n    }\n}\n````\n\nIf your code will contain many keys, consider using the [HashedKeyedLocker](/KeyedLock/HashedKeyedLocker.cs) with a hash function to minimize the nubmer of \n[SemaphoreSlim](https://docs.microsoft.com/en-us/dotnet/api/system.threading.semaphoreslim?view=netstandard-2.0)\nobjects that will be held in memory.\n\n````csharp\npublic class Foo\n{\n    private HashedKeyedLock\u003clong, int\u003e _locker = new HashedKeyedLocker\u003clong, int\u003e(long x =\u003e (int)(x % 10)); // limit to 10 locks in memory\n  \n    public void DoSomethingWithLock(long keyToLock)\n    {\n        _locker[keyToLock].Wait();\n        try\n        {\n            //do something...\n        }\n        finally\n        {\n            _locker[keyToLock].Release();\n        }\n    }\n}\n````\n## Resources\nSome of the ideas for this repo were taken from [this blog post](https://www.tabsoverspaces.com/233703-named-locks-using-monitor-in-net-implementation).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoug144%2Fkeyedlocks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdoug144%2Fkeyedlocks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoug144%2Fkeyedlocks/lists"}