{"id":95965,"date":"2017-04-14T07:00:00","date_gmt":"2017-04-14T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=95965"},"modified":"2019-03-13T01:09:35","modified_gmt":"2019-03-13T08:09:35","slug":"20170414-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170414-00\/?p=95965","title":{"rendered":"How can I atomically leave a critical section and delete it?"},"content":{"rendered":"<p>A customer had a thread which has entered a critical section. It has finished whatever it needs to do, and now it wants to leave the critical section and delete it. But wait, there&#8217;s a race condition here: Between the call to <code>Leave&shy;Critical&shy;Section<\/code> and <code>Delete&shy;Critical&shy;Section<\/code>, another thread might sneak in and enter the critical section. This means that the original thread is going to delete a critical section while there are threads waiting for it, which the documentation <a HREF=\"https:\/\/msdn.microsoft.com\/library\/windows\/desktop\/ms682552(v=vs.85).aspx\">explicitly calls out as leaving the waiting threads in an undefined state<\/a>. <\/p>\n<p>The question then is how to close this race window by performing the leave and delete atomically? <\/p>\n<p>Actually, that&#8217;s the wrong question, because having such an atomic operation doesn&#8217;t fix anything. Suppose your thread calls that atomic leave-and-delete function. Now, the other interloper thread cannot enter the critical section after you leave and before you delete it. Yay, the race condition is gone! <\/p>\n<p>But wait, you jumped out of the frying pan and landed in the fire: What&#8217;s going to happen is that the interloper thread will instead try to enter the critical section <i>after it has been deleted<\/i>, which also results in undefined behavior. <\/p>\n<p>All you did was trade one undefined behavior for another. You didn&#8217;t actually fix anything. <\/p>\n<p>If you have a system set up where there&#8217;s the possibility of a thread entering a critical section that is about to be deleted, then it means that you also have the possibility of a thread entering a critical section <i>after<\/i> it has been deleted. <\/p>\n<p>So fix your design so that second problem no longer exists. Once you fix that, you&#8217;ll see that the original problem also vanishes. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Even if you could, it wouldn&#8217;t help you.<\/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-95965","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Even if you could, it wouldn&#8217;t help you.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/95965","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=95965"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/95965\/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=95965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=95965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=95965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}