{"id":94885,"date":"2016-12-08T07:00:00","date_gmt":"2016-12-08T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=94885"},"modified":"2019-03-13T10:34:40","modified_gmt":"2019-03-13T17:34:40","slug":"20161208-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20161208-00\/?p=94885\/","title":{"rendered":"What is this race condition that the OpenMutex documentation is trying to warn me about?"},"content":{"rendered":"<p>A customer asked for clarification on what they considered an enigmatic sentence in <a HREF=\"https:\/\/msdn.microsoft.com\/library\/windows\/desktop\/ms684315(v=vs.85).aspx\">the documentation for the <code>Open&shy;Mutex<\/code> function<\/a>: <\/p>\n<blockquote CLASS=\"q\"><p>If your multithreaded application must repeatedly create, open, and close a named mutex object, a race condition can occur. In this situation, it is better to use <b>Create&shy;Mutex<\/b> instead of <b>Open&shy;Mutex<\/b>, because <b>Create&shy;Mutex<\/b> opens a mutex if it exists and creates it if it does not. <\/p><\/blockquote>\n<p>&#8220;What is this race condition the documentation is talking about? Our program uses <code>Open&shy;Mutex<\/code> and we are wondering if we should switch to <code>Create&shy;Mutex<\/code>.&#8221; <\/p>\n<p>Consider two threads. One thread calls <code>Create&shy;Mutex<\/code>, then <code>Close&shy;Handle<\/code>, then <code>Create&shy;Mutex<\/code>, then <code>Close&shy;Handle<\/code>, then <code>Create&shy;Mutex<\/code>, then <code>Close&shy;Handle<\/code>, and so on. <\/p>\n<p>The other thread calls <code>Open&shy;Mutex<\/code>. <\/p>\n<p>The race condition is that the second thread&#8217;s call to <code>Open&shy;Mutex<\/code> will fail if it takes place after the first thread calls <code>Close&shy;Handle<\/code> and before it gets to make its next call to <code>Create&shy;Mutex<\/code>. <\/p>\n<p>One of my colleagues couldn&#8217;t understand why MSDN bothers to say anything about this situation at all. &#8220;You can&#8217;t open a mutex that doesn&#8217;t exist. Duh. I think this adds more confusion than it helps.&#8221; <\/p>\n<p>I suspect the reason why MSDN bothers to say anything about this is that there was a customer who had two threads. One thread calls <code>Create&shy;Mutex<\/code>, then <code>Close&shy;Handle<\/code>, then <code>Create&shy;Mutex<\/code>, then <code>Close&shy;Handle<\/code>, then <code>Create&shy;Mutex<\/code>, then <code>Close&shy;Handle<\/code>, and so on. The other thread calls <code>Open&shy;Mutex<\/code>. <\/p>\n<p>This customer found that if the second thread calls <code>Open&shy;Mutex<\/code> at an inopportune time, the call fails. They then insisted that something be added to the documentation to <a HREF=\"https:\/\/technet.microsoft.com\/magazine\/jj643252.aspx\">state explicitly that a bad idea is a bad idea<\/a>. Probably because they needed something in writing to show their management in order to justify the time they are going to need to spend fixing the bug. <\/p>\n<p>Once again, MSDN has been forced into <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20160519-00\/?p=93495\">being a pawn in some company&#8217;s internal politics<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Open it, but it won&#8217;t come.<\/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-94885","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Open it, but it won&#8217;t come.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94885","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=94885"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94885\/revisions"}],"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=94885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=94885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=94885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}