{"id":111421,"date":"2025-07-30T07:00:00","date_gmt":"2025-07-30T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=111421"},"modified":"2025-07-30T09:58:22","modified_gmt":"2025-07-30T16:58:22","slug":"20250730-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20250730-00\/?p=111421","title":{"rendered":"In C++\/WinRT, how can I await multiple coroutines and capture the results?, part 0"},"content":{"rendered":"<p>A while ago, I investigated ways of <a title=\"In C++\/WinRT, how can I await multiple coroutines and capture the results?, part 1\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240110-00\/?p=109256\"> awaiting multiple C++\/WinRT corotuines and capturing the results<\/a>. I started with <code>winrt::<wbr \/>when_all()<\/code>, but really I could have started even earlier.<\/p>\n<p>The easy way to await multiple coroutines is to await each one!<\/p>\n<pre>auto op1 = DoSomething1Async();\r\nauto op2 = DoSomething2Async();\r\n\r\nauto result1 = co_await op1;\r\nauto result2 = co_await op2;\r\n<\/pre>\n<p>This starts two operations, and only after they have started do we wait for them in sequence.<\/p>\n<p>This works great, as long as you understand that if an exception occurs in <code>co_await op1<\/code>, then the second operation <code>op2<\/code> just keeps on running and eventually has nobody to report results to.<\/p>\n<p>This is probably okay for the most part: The second operation just keeps on running pointlessly. Not necessarily the most efficient, but probably not harmful.<\/p>\n<p>If you want to cancel the second operation if the first operation fails, you can do that:<\/p>\n<pre>auto op1 = DoSomething1Async();\r\nauto op2 = DoSomething2Async();\r\n\r\nauto op2cancel = wil::scope_exit([&amp;] {\r\n    op2.Cancel();\r\n});\r\nauto result1 = co_await op1;\r\nop2cancel.release(); \/\/ don't cancel it after all\r\nauto result2 = co_await op2;\r\n<\/pre>\n<p>Note also that in the case where <code>op2<\/code> finishes before <code>op1<\/code>, this code fragment doesn&#8217;t learn about it right away because it&#8217;s busy waiting for <code>op1<\/code>. Again, this is not normally a problem.<\/p>\n<p>So if all you care about is getting both results, and you&#8217;re not too worried about what happens if one of them fails, you can just <code>co_await<\/code> each one in turn. Pretty simple and straightforward. All the extra complexity I wrote about is trying to solve a problem you don&#8217;t care about.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you don&#8217;t really care about what happens if something goes wrong.<\/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-111421","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>If you don&#8217;t really care about what happens if something goes wrong.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111421","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=111421"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111421\/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=111421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=111421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=111421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}