{"id":9387,"date":"2025-03-31T12:33:13","date_gmt":"2025-03-31T11:33:13","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=9387"},"modified":"2025-04-10T11:27:55","modified_gmt":"2025-04-10T10:27:55","slug":"c-keyedsemaphore","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-keyedsemaphore\/","title":{"rendered":"C# KeyedSemaphore"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">using System;\nusing System.Collections.Concurrent;\nusing System.Threading;\nusing System.Threading.Tasks;\n\npublic class KeyedSemaphore\n{\n    private readonly ConcurrentDictionary&lt;string, Lazy&lt;SemaphoreSlim>> _semaphores =\n        new ConcurrentDictionary&lt;string, Lazy&lt;SemaphoreSlim>>();\n\n    public async Task&lt;IDisposable> LockAsync(string key)\n    {\n        var semaphore = _semaphores.GetOrAdd(key, k => new Lazy&lt;SemaphoreSlim>(() => new SemaphoreSlim(1, 1))).Value;\n        await semaphore.WaitAsync();\n        return new Releaser(this, key);\n    }\n\n    private void Release(string key)\n    {\n        if (_semaphores.TryGetValue(key, out var lazySemaphore))\n        {\n            var semaphore = lazySemaphore.Value;\n            semaphore.Release();\n\n            \/\/ Opruimen als er geen wachtende taken meer zijn\n            if (semaphore.CurrentCount == 1)\n            {\n                _semaphores.TryRemove(key, out _);\n            }\n        }\n    }\n\n    private class Releaser : IDisposable\n    {\n        private readonly KeyedSemaphore _keyedSemaphore;\n        private readonly string _key;\n\n        public Releaser(KeyedSemaphore keyedSemaphore, string key)\n        {\n            _keyedSemaphore = keyedSemaphore;\n            _key = key;\n        }\n\n        public void Dispose()\n        {\n            _keyedSemaphore.Release(_key);\n        }\n    }\n}\n<\/pre><\/div>\n\n\n\n<p>Or use a library like: <a href=\"https:\/\/github.com\/amoerie\/keyed-semaphores\">https:\/\/github.com\/amoerie\/keyed-semaphores<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Or use a library like: https:\/\/github.com\/amoerie\/keyed-semaphores<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9387","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/9387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=9387"}],"version-history":[{"count":2,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/9387\/revisions"}],"predecessor-version":[{"id":9391,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/9387\/revisions\/9391"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=9387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=9387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=9387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}