{"id":96396,"date":"2017-06-15T07:00:00","date_gmt":"2017-06-15T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=96396"},"modified":"2019-03-13T01:12:54","modified_gmt":"2019-03-13T08:12:54","slug":"20170615-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170615-00\/?p=96396","title":{"rendered":"Creating an automatic-reset event from WaitOnAddress"},"content":{"rendered":"<p>Last time, we <!-- backref: Creating a manual-reset event from WaitOnAddress -->created a manual-reset event from <code>Wait&shy;On&shy;Address<\/code><\/a>. Today, it&#8217;s an automatic-reset event. <\/p>\n<pre>\nstruct ALT_AEVENT\n{\n  LONG State;\n};\n\nvoid InitializeAltAutoEvent(ALT_AEVENT* Event,\n                            bool InitialState)\n{\n  Semaphore-&gt;State = InitialState;\n}\n\nvoid SetAltAutoEvent(ALT_AEVENT* Event)\n{\n if (InterlockedCompareExchange(&amp;Event-&gt;State,\n                                true, false) == false) {\n  WakeByAddressSingle(&amp;Event-&gt;State);\n }\n}\n\nvoid ResetAltAutoEvent(ALT_AEVENT* Event)\n{\n InterlockedCompareExchange(&amp;Event-&gt;State,\n                            false, true);\n}\n\nvoid WaitForAltAutoEvent(ALT_AEVENT* Event)\n{\n while (!InterlockedCompareExchange(&amp;Event-&gt;State,\n                                    false, true)) {\n  LONG Waiting = 0;\n  WaitOnAddress(&amp;Event-&gt;State,\n                &amp;Waiting,\n                sizeof(Waiting),\n                INFINITE);\n }\n}\n<\/pre>\n<p>Most of this code is the same as with manual-reset events. One difference is that when setting the event, we use <code>Wake&shy;By&shy;Address&shy;Single<\/code> because signaling an auto-reset event releases at most one thread. <\/p>\n<p>The interesting change is in thw code that waits. Instead of merely checking the state, we try to transition it from <code>true<\/code> to <code>false<\/code>, which simultaneously checks and claims the token. <\/p>\n<p>Okay, next time, we&#8217;re going to put together what we&#8217;ve been learning this week to solve an actual problem. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Completing the quartet.<\/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-96396","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Completing the quartet.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96396","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=96396"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96396\/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=96396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=96396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=96396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}