{"id":103584,"date":"2020-03-23T07:00:00","date_gmt":"2020-03-23T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103584"},"modified":"2020-03-22T09:02:57","modified_gmt":"2020-03-22T16:02:57","slug":"20200323-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200323-00\/?p=103584\/","title":{"rendered":"Why not just share a single event across all critical section?"},"content":{"rendered":"<p>Neil Rashbrook wonder why <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20191101-00\/?p=103046#comment-135674\"> there isn&#8217;t a single event that all critical sections share<\/a>. When a critical section is exited, and there is a waiting thread, then &#8220;signal all threads waiting on critical sections&#8221;. One thread will get the critical section, and threads that are waiting for unrelated critical sections will just think that they lost a nonexistent race and go back to sleep.<\/p>\n<p>There are a few problems with this idea.<\/p>\n<p>The first problem is specific to the way Windows kernel events work. How would you &#8220;signal all threads waiting on critical sections&#8221; with a kernel event? If you use an automatic-reset event, then only one thread will wake. In order to wake all threads, you need a manual-reset event. But how do you know when to reset the event? You want to reset the event once all threads have woken up, but you have no way of knowing when that has happened.<\/p>\n<p>You might think of using <code>Pulse\u00adEvent<\/code> and then realize that you have no easy way of closing the race condition between a thread realizing that it needs to go to sleep and the actual sleep. You would have to use something like <code>Signal\u00adObject\u00adAnd\u00adWait<\/code>, but that means that every critical section acquisition would need to take a kernel mutex so it could prevent the owner from pulsing the event before the waiter could reach the <code>Wait\u00adFor\u00adSingle\u00adObject<\/code> on the kernel event. But if you&#8217;re going to take a kernel mutex on every acquisition, then you destroyed the purpose of the critical section, which is to be a lightweight alternative to a kernel mutex! You may as well just use a kernel mutex as your critical section.<\/p>\n<p>And all that is on top of the fact that <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050105-00\/?p=36803\"> <code>Pulse\u00adEvent<\/code> is fundamentally flawed<\/a>.<\/p>\n<p>Now, maybe you could come up with an alternative to a kernel mutex and a kernel event. Say, a global slim reader-writer lock and a paired condition variable. The condition variable lets you <code>notify_all<\/code> in a reliable way, and then every thread checks whether their critical section is available. Could we use that for our critical section design?<\/p>\n<p>I guess you could do that, but you wouldn&#8217;t want to. Waking every thread that&#8217;s waiting for a critical section, even though you know that only one will succeed, is the definition of a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Thundering_herd_problem\"> thundering herd problem<\/a>. You take a lot of context switches, lose a lot of CPU cache, page in a lot of memory, and waste a lot of CPU time with all the pointless work. You really want to wake just one candidate thread and leave the others sleeping. That&#8217;s why our <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20160825-00\/?p=94165\"> critical section built out of <code>Wait\u00adOn\u00adAddress<\/code><\/a> uses <code>Wake\u00adBy\u00adAddress\u00adSingle<\/code>.<\/p>\n<p>Note that the candidate thread you wake up may not actually succeded at claiming the critical section, because another thread may sneak in and claim the critical section before your candidate can wake up. Although this sounds unfair, unfairness is actually a feature, because it avoids <a href=\"https:\/\/en.wikipedia.org\/wiki\/Lock_convoy\"> lock convoys<\/a>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How will you know when the thundering herd has calmed down?<\/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-103584","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>How will you know when the thundering herd has calmed down?<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103584","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=103584"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103584\/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=103584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}