{"id":112778,"date":"2026-10-16T07:00:00","date_gmt":"2026-10-16T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=112778"},"modified":"2026-10-15T12:18:04","modified_gmt":"2026-10-15T19:18:04","slug":"20261016-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20261016-00\/?p=112778\/","title":{"rendered":"C++ futures part 5: <CODE>std::<WBR>future<\/CODE> and <CODE>std::<WBR>shared_future<\/CODE>"},"content":{"rendered":"<p><a title=\"C++ futures part 4: What if I want an immediate on-thread std::async?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20261015-00\/?p=112776\"> Wrapping up our quick introduction to the C++ futures library<\/a>, we come to the last two parts: <code>std::<wbr \/>future<\/code> and <code>std::<wbr \/>shared_future<\/code>.<\/p>\n<p>The <code>std::<wbr \/>future<\/code> is the starting point. It is a move-only class that this takes the <code>source<\/code> and forwards its <code>wait<\/code> and <code>wait_for<\/code> methods to the <code>source<\/code>, and forwards its <code>get<\/code> method to <code>consume<\/code>.<\/p>\n<p>Forwarding the <code>get<\/code> to <code>consume<\/code> means that you can call <code>get<\/code> only once.<\/p>\n<p>The idea is that <code>std::<wbr \/>future<\/code> is for the case where there is exactly one consumer of the value. Since it is move-only, you can&#8217;t copy it; you can only give it away and transfer the power to retrieve the value to the recipient.<\/p>\n<p>The <code>std::<wbr \/>future<\/code> has a method called <code>shared()<\/code>. This transfers the <code>source<\/code> from the <code>future<\/code> to a <code>shared_<wbr \/>future<\/code> object. The <code>shared_<wbr \/>future<\/code> also forwards its <code>wait<\/code> and <code>wait_for<\/code> methods to the <code>source<\/code>, but instead of forwarding its <code>get<\/code> to the <code>source::<wbr \/>consume<\/code>, <code>shared_<wbr \/>future::<wbr \/>get()<\/code> just calls <code>source::<wbr \/>get<\/code> to get a reference to the value storage.<\/p>\n<p>This is what makes the &#8220;shared future&#8221; shared: The <code>shared_<wbr \/>future<\/code> is copyable, so you can have multiple people all with their own <code>shared_<wbr \/>future<\/code> that share the same <code>source<\/code>, and each one can separately wait for the value and retrieve the value as many times as they want.<\/p>\n<p>The downside of <code>shared_<wbr \/>future<\/code> is that you always get a const reference, so if you want to perform non-const operations on the result, you&#8217;ll have to make a copy and operate on the copy. And that&#8217;s not possible at all if the result is itself a move-only type, since you can&#8217;t move out of a const reference.<\/p>\n<p>You can see that at the end of the day, all of the pieces of the futures library are just different ways of manipulating the invisible <code>source<\/code>. Some components push values into the <code>source<\/code>, and others pull values out of the <code>source<\/code>.<\/p>\n<p>It took me a while to realize that the most important part of the whole futures library is something you can&#8217;t even see.<\/p>\n<p><b>Bonus chatter<\/b>: All the names in the futures library are horrible. I mean, really, &#8220;packaged task&#8221;?<\/p>\n<p>Here&#8217;s my attempt to give the pieces better names:<\/p>\n<table style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th>Old name<\/th>\n<th>New name<\/th>\n<\/tr>\n<tr>\n<td>promise<\/td>\n<td>promise_source<\/td>\n<\/tr>\n<tr>\n<td>packaged_task<\/td>\n<td>function_promise_source<\/td>\n<\/tr>\n<tr>\n<td>async(deferred)<\/td>\n<td>make_lazy_promise<\/td>\n<\/tr>\n<tr>\n<td>async(async)<\/td>\n<td>make_thread_promise<\/td>\n<\/tr>\n<tr>\n<td>future<\/td>\n<td>promise<\/td>\n<\/tr>\n<tr>\n<td>shared_future<\/td>\n<td>shared_promise<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>I admit, these names aren&#8217;t all that good either. But I prefer to use the word &#8220;promise&#8221; to refer to the thing that &#8220;promises&#8221; to produce a value. And the pattern <code>promise_source<\/code> \/ <code>promise<\/code> makes the relationship between the two much clearer: A promise source is a thing that makes promises. And it avoids the hilarity of a non-async <code>async()<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Getting the results back out.<\/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-112778","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Getting the results back out.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112778","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=112778"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112778\/revisions"}],"predecessor-version":[{"id":112779,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112778\/revisions\/112779"}],"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=112778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=112778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=112778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}