{"id":105863,"date":"2021-11-01T07:00:00","date_gmt":"2021-11-01T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=105863"},"modified":"2021-11-01T06:53:18","modified_gmt":"2021-11-01T13:53:18","slug":"20211101-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20211101-00\/?p=105863","title":{"rendered":"Why do all this coroutine stuff if you can just use <CODE>std::future<\/CODE>?"},"content":{"rendered":"<p>One of my colleagues reacted to <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210504-01\/?p=105178\"> my series on creating custom coroutines<\/a> by asking, &#8220;Why did you bother with this <code>simple_task<\/code> custom coroutine? Why not just use <code>std::future<\/code> objects produced by <code>std::async<\/code>? You can <code>co_await<\/code> a <code>std::future<\/code>.&#8221;<\/p>\n<p>Okay, first of all, the ability to <code>co_await<\/code> a <code>std::future<\/code> is not a feature of the standard (as of this writing). It&#8217;s something that the Microsoft STL provides as a courtesy. And that courtesy has <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200916-00\/?p=104227\"> not always been smooth sailing<\/a>.<\/p>\n<p>For another thing, <code>std::async<\/code> lets you pick how the lambda runs, but your only options are to run it on a separate thread, or to run the lambda only when the value is awaited. The lambda itself can&#8217;t do any <code>co_await<\/code>ing because it is not a coroutine. It&#8217;s just a plain synchronous lambda.<\/p>\n<p>But the real kicker is that the Microsoft STL implementation of <code>std::future<\/code> is written in\u2026 PPL, The very library we are trying to get rid of by switching to coroutines in the first place.<\/p>\n<p>That last bit was the clincher as far as my colleague was concerned. He sent me the <a href=\"https:\/\/www.reddit.com\/r\/memes\/comments\/fzmn27\/always_has_been\/\"> astronaut with a gun meme<\/a>, but instead of asking, &#8220;Wait, it&#8217;s all Ohio?&#8221;, the first astronaut asks, &#8220;std::future is just PPL?&#8221; The reply from the astronaut with the gun remains &#8220;Always has been.&#8221;<\/p>\n<p><b>Meme background<\/b>: <a href=\"https:\/\/knowyourmeme.com\/memes\/wait-its-all-ohio-always-has-been\"> Wait it&#8217;s all Ohio? Always has been<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Escape from PPL.<\/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-105863","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Escape from PPL.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105863","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=105863"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105863\/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=105863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=105863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=105863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}