{"id":109707,"date":"2024-04-29T07:00:00","date_gmt":"2024-04-29T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109707"},"modified":"2024-04-29T08:54:27","modified_gmt":"2024-04-29T15:54:27","slug":"20240429-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240429-00\/?p=109707","title":{"rendered":"Awaiting a set of handles in C++\/WinRT"},"content":{"rendered":"<p>C++\/WinRT provides the <code>resume_<wbr \/>on_<wbr \/>signal<\/code> awaiter that allows you to await until a kernel handle is signaled. What if you have a bunch of these handles, and you want to await until <i>all<\/i> of them are signaled?<\/p>\n<p>It turns out that this is easier than it sounds. We can use the same trick as we used in <a title=\"Synthesizing a when_all coroutine from pieces you already have\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200902-00\/?p=104155\"> our basic <code>when_<wbr \/>all<\/code> function<\/a>: Just await each handle in sequence.<\/p>\n<pre>winrt::Windows::Foundation::IAsyncAction\r\n    when_all_signaled(std::vector&lt;HANDLE&gt; handles)\r\n{\r\n    for (auto handle : handles) {\r\n        co_await winrt::resume_on_signal(handle);\r\n    }\r\n}\r\n<\/pre>\n<p>If you want to accept the handles varadically, then it&#8217;s the one-liner we saw before:<\/p>\n<pre>template&lt;typename... Handles&gt;\r\nwinrt::Windows::Foundation::IAsyncAction\r\n    when_all_signaled(Handles... handles)\r\n{\r\n    (co_await winrt::resume_on_signal(handles), ...);\r\n}\r\n<\/pre>\n<p>Things get more complicated if we want to await a set of handles with a timeout. That&#8217;s what we&#8217;ll be looking at for the next few days.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s easier thank you think.<\/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-109707","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It&#8217;s easier thank you think.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109707","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=109707"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109707\/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=109707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}