{"id":17823,"date":"2009-06-19T10:00:00","date_gmt":"2009-06-19T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2009\/06\/19\/you-cant-leave-a-critical-section-you-never-entered\/"},"modified":"2009-06-19T10:00:00","modified_gmt":"2009-06-19T10:00:00","slug":"you-cant-leave-a-critical-section-you-never-entered","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20090619-00\/?p=17823","title":{"rendered":"You can&#8217;t leave a critical section you never entered"},"content":{"rendered":"<p>If you call <code>LeaveCriticalSection<\/code> on a critical section you never entered, the behavior is undefined.<\/p>\n<p> Critical sections are an extremly high-traffic code path. Intense work goes into making them as fast as possible. Customers like to ask questions like &#8220;Exactly how many cycles does it take to enter a critical section? We&#8217;re not going to use them if they&#8217;re too slow.&#8221; They don&#8217;t say what they are going to do if critical sections are too slow for them, but the threat is real (even if the justification is bogus). I&#8217;ve seen programmers respond to code feedback of the form &#8220;You need a critical section here&#8221; with &#8220;No, I&#8217;m not going to bother. I&#8217;m afraid it&#8217;ll slow down my code.&#8221; <\/p>\n<p> Given that critical sections are so heavily used, the algorithm gets tweaked regularly in order to improve performance. Sometimes the tweaks are minor; other times, the algorithm gets a major overhaul. Of course, the hope is that <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/05\/25\/141253.aspx\"> by changing only the insides, nobody will notice<\/a>. On the other hand, <a href=\"http:\/\/blogs.codegear.com\/abauer\/2008\/02\/06\/38855\/\"> people who relied on undefined behavior<\/a> (like exiting a critical section they never entered and hoping that something meaningful would happen) are going to see changes in behavior. <\/p>\n<p> I don&#8217;t know the precise reasons why the internals of critical sections changed, but I suspect it had to do with <a href=\"http:\/\/www.bluebytesoftware.com\/blog\/PermaLink,guid,e40c2675-43a3-410f-8f85-616ef7b031aa.aspx\"> mitigating the effect of lock convoy<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you call LeaveCriticalSection on a critical section you never entered, the behavior is undefined. Critical sections are an extremly high-traffic code path. Intense work goes into making them as fast as possible. Customers like to ask questions like &#8220;Exactly how many cycles does it take to enter a critical section? We&#8217;re not going to [&hellip;]<\/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-17823","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>If you call LeaveCriticalSection on a critical section you never entered, the behavior is undefined. Critical sections are an extremly high-traffic code path. Intense work goes into making them as fast as possible. Customers like to ask questions like &#8220;Exactly how many cycles does it take to enter a critical section? We&#8217;re not going to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/17823","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=17823"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/17823\/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=17823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=17823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=17823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}