{"id":112278,"date":"2026-04-28T07:00:00","date_gmt":"2026-04-28T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=112278"},"modified":"2026-04-29T08:59:47","modified_gmt":"2026-04-29T15:59:47","slug":"20260428-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260428-00\/?p=112278","title":{"rendered":"Developing a cross-process reader\/writer lock with limited readers, part 1: A semaphore"},"content":{"rendered":"<p>Say you want to have the functionality of a reader\/writer lock, but have it work cross-process. The built-in <code>SRWLOCK<\/code> works only within a single process. Can we build a reader\/writer lock that works across processes?<\/p>\n<p>For convenience, let&#8217;s say that you want to support a maximum of <var>N<\/var> simultaneous readers, for some fixed value <var>N<\/var>. We can do this:<\/p>\n<ul>\n<li>Create a semaphore with a token count of <var>N<\/var>. Share this semaphore with all of the processes, either by giving it a name or by duplicating the handle into each of the processes.<\/li>\n<li>To take a read lock, claim one token from the semaphore. To release the lock, release the token.<\/li>\n<li>To take a write lock, claim <var>N<\/var> tokens from the semaphore. To release the lock, release <var>N<\/var> tokens.<\/li>\n<\/ul>\n<p>The idea for the write lock is that it&#8217;s accomplished by claiming all the read locks, thereby ensuring that nobody else can get a read lock.<\/p>\n<pre>#define MAX_SHARED 100\r\nHANDLE sharedSemaphore;\r\n\r\nvoid AcquireShared()\r\n{\r\n    WaitForSingleObject(sharedSemaphore, INFINITE);\r\n}\r\n\r\nvoid ReleaseShared()\r\n{\r\n    ReleaseSemaphore(sharedSemaphore, 1, nullptr);\r\n}\r\n\r\nvoid AcquireExclusive()\r\n{\r\n    for (unsigned i = 0; i &lt; MAX_SHARED; i++) {\r\n        WaitForSingleObject(sharedSemaphore, INFINITE);\r\n    }\r\n}\r\n\r\nvoid ReleaseExclusive()\r\n{\r\n    ReleaseSemaphore(sharedSemaphore, MAX_SHARED, nullptr);\r\n}\r\n<\/pre>\n<p>Since we are using <code>Wait\u00adFor\u00adSingle\u00adObject<\/code>, we can also add a timeout, so that the caller can decide to abandon the operation if they can&#8217;t claim the lock.<\/p>\n<pre>bool AcquireSharedWithTimeout(DWORD timeout)\r\n{\r\n    return WaitForSingleObject(sharedSemaphore, timeout) == WAIT_OBJECT_0;\r\n}\r\n\r\nbool AcquireExclusiveWithTimeout(DWORD timeout)\r\n{\r\n    DWORD start = GetTickCount();\r\n    for (unsigned i = 0; i &lt; MAX_SHARED; i++) {\r\n        DWORD elapsed = GetTickCount() - start;\r\n        if (elapsed &gt; timeout ||\r\n            WaitForSingleObject(sharedSemaphore, timeout - elapsed) == WAIT_TIMEOUT)) {\r\n            \/\/ Restore the tokens we already claimed.\r\n            if (i &gt; 0) {\r\n                ReleaseSemaphore(sharedSemaphore, i, nullptr);\r\n            }\r\n            return false;\r\n        }\r\n    }\r\n    return true;\r\n}\r\n<\/pre>\n<p>Exclusive acquisition is tricky because we have to call <code>Wait\u00adFor\u00adSingle\u00adObject<\/code> multiple times, with decreasing timeouts as time passes. If we run out of time, then we need to give back the tokens we had prematurely claimed.<\/p>\n<p>There&#8217;s still a problem here. We&#8217;ll look at it next time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A pot of tokens.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-112278","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A pot of tokens.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=112278"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112278\/revisions"}],"predecessor-version":[{"id":112282,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112278\/revisions\/112282"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=112278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=112278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=112278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}