<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	
  <title>The Old New Thing - Dreamcatcher Edition</title>
	<atom:link href="https://devblogs.microsoft.com/oldnewthing/feed/" rel="self" type="application/rss+xml" />
	<link>https://devblogs.microsoft.com/oldnewthing/</link>
	<description>Practical development throughout the evolution of Windows.</description>
	<lastBuildDate>Thu, 22 Oct 2026 16:29:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2021/03/Microsoft-Favicon.png</url>
	
  <title>The Old New Thing - Dreamcatcher Edition</title>
	<link>https://devblogs.microsoft.com/oldnewthing/</link>
	<width>32</width>
	<height>32</height>
</image> 
	
  <item>
    <title>Why didn&#8217;t we use Ro&#173;Register&#173;For&#173;Apartment&#173;Shutdown to clean up the unmarshalable delegate?</title>
    <link>https://devblogs.microsoft.com/oldnewthing/20261021-00/?p=112786/</link>
    <comments>https://devblogs.microsoft.com/oldnewthing/20261021-00/?p=112786/#respond</comments>
    <dc:creator><![CDATA[Raymond Chen]]></dc:creator>
    <pubDate>Wed, 21 Oct 2026 14:00:00 +0000</pubDate>
    <category><![CDATA[Old New Thing]]></category>
    <category><![CDATA[Code]]></category>
    <guid isPermaLink="false">https://devblogs.microsoft.com/oldnewthing/?p=112786</guid>
    <content:encoded><![CDATA[<p>In my attempt to <a title="Creating a fake agile wrapper that is technically agile but is not useful outside its home apartment" href="https://devblogs.microsoft.com/oldnewthing/20260803-00/?p=112582"> create a fake agile wrapper that is technically agile but is not useful outside its home apartment</a>, <a href="https://devblogs.microsoft.com/oldnewthing/20260803-00/?p=112582&amp;commentid=144559#comment-144559"> Luca Bacci suggested, among other things</a>, using the <code>Ro­Register­For­Apartment­Shutdown</code> function.</p>
<p>The <code>Ro­Register­For­Apartment­Shutdown</code> function is nice, but it doesn&#8217;t suit our needs.</p>
<p>One problem is that the object registered for apartment shutdown must be agile. (If you pass a non-agile object, the call fails.) That means that our wrapper must be agile. If we put the non-marshalable non-agile object inside our wrapper, we now have an agile object (the wrapper) that holds a non-agile object (the original object) as a member, which creates the risk that the wrapper will run down in the wrong apartment. The normal way of fixing that is to use <code>Ro­Get­Agile­Reference</code> to create an agile wrapper around the non-agile object, and store that agile wrapper in our container object.</p>
<p>This puts us back where we started: We are trying to implement that agile wrapper!</p>
<p>Even if we got past that problem, we have another problem: At apartment shutdown, COM calls the wrapper object&#8217;s <code>UnInitialize</code> method, and there&#8217;s a warning on the using the <code>Ro­Unregister­For­Apartment­Shutdown</code> function:</p>
<blockquote class="q"><p><b>Warning</b>: Don&#8217;t call the <code>Ro­Unregister­For­Apartment­Shutdown</code> function from the <code>OnUninitialize</code> callback.</p></blockquote>
<p>But wait, what if the destructor of the object we are wrapping wants to call <code>Ro­Unregister­For­Apartment­Shutdown</code>? It might happen indirectly because it has its own reference to another object, that in turn tries to call <code>Ro­Unregister­For­Apartment­Shutdown</code>.</p>
<p>For example, it might hold another fake agile wrapper whose <code>UnInitialize</code> method has already been called! (We can work around that in our agile wrapper by nulling out the non-agile pointer so we don&#8217;t double-release, but maybe there&#8217;s some other object that uses <code>Ro­Unregister­For­Apartment­Shutdown</code> that is less resilient to this scenario.)</p>
<p>Another requirement of <code>Ro­Unregister­For­Apartment­Shutdown</code> is that it must be called from the same apartment that registered. This doesn&#8217;t quite put us back where we started, but it makes things more complicated because the whole point of the exercise is to find a way to release the object without having to try to switch to the apartment. I guess what we could do is use <code>IContext­Callback</code> to try to switch back to the apartment to release the original object and unregister the apartment shutdown callback. If the apartment switch fails, then we assume it&#8217;s because the apartment has already run down, so we assume that the <code>IApartment­Shutdown::<wbr />On­Uninitialize</code> method was called, and it released the object for us. (Of course, that still creates the problem that we are cleaning up inside the <code>On­Uninitialize</code> callback, which meas that nobody else can unregister their apartment shutdown callback.)</p>
<p>The last note on this is that the implementation is written on the assumption that apartment shutdown callbacks are rarely unregistered. Registration is <var>O</var>(1), but unregistration is <var>O</var>(<var>n</var>) because it does a linear search through a linked list. By comparison, the Global Interface Table is designed on the assumption that objects are frequently registered, unregistered, and looked up, so it can look up its cookies in <var>O</var>(1) time. The proposed alternate design would unregister a lot of apartment shutdown callbacks, so it&#8217;s pushing the apartment shutdown callback list into a usage pattern it wasn&#8217;t optimized for.</p>
<p>The post <a href="https://devblogs.microsoft.com/oldnewthing/20261021-00/?p=112786/">Why didn&#8217;t we use &lt;CODE&gt;Ro&shy;Register&shy;For&shy;Apartment&shy;Shutdown&lt;/CODE&gt; to clean up the unmarshalable delegate?</a> appeared first on <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a>.</p>
]]></content:encoded>
    <wfw:commentRss>https://devblogs.microsoft.com/oldnewthing/20261021-00/?p=112786/feed/</wfw:commentRss>
    <slash:comments>0</slash:comments>
    <image type="image/png" url="https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2025/10/banner-oldnewthing-blue.webp"/>
  </item>
		
  <item>
    <title>Microspeak: Drain the slide, back-up slides</title>
    <link>https://devblogs.microsoft.com/oldnewthing/20261020-00/?p=112784/</link>
    <comments>https://devblogs.microsoft.com/oldnewthing/20261020-00/?p=112784/#respond</comments>
    <dc:creator><![CDATA[Raymond Chen]]></dc:creator>
    <pubDate>Tue, 20 Oct 2026 14:00:00 +0000</pubDate>
    <category><![CDATA[Old New Thing]]></category>
    <category><![CDATA[Microspeak]]></category>
    <category><![CDATA[Other]]></category>
    <guid isPermaLink="false">https://devblogs.microsoft.com/oldnewthing/?p=112784</guid>
    <content:encoded><![CDATA[<p>Recall that Microspeak is not merely for jargon exclusive to Microsoft, but it&#8217;s jargon that you need to know to survive at Microsoft. Even if you don&#8217;t use it yourself, you need to know what it means when others say it.</p>
<p>To <i>drain the slide</i> is, <a title="Corporate term: Drain the slide" href="https://stickfight.co.uk/2024/03/29/corporate-term-drain-the-slide/"> as Mark Myers explains it</a>, reading the contents of a slide to the audience when giving a presentation. At least at Microsoft, the term is frequently used by a speaker in the negative, such as &#8220;I don&#8217;t want to drain the slide here&#8230;&#8221;</p>
<p>My theory is that the phenomenon of draining the slide has multiple sources.</p>
<p>One is simply the result of inexperience on the part of the presenter. They don&#8217;t know what to say when giving a presentation, so they end up just reading out the text that they wrote on the slide.</p>
<p>A more insidious source is the result of tension between the multiple reason for creating a slide deck.</p>
<p>One reason to create a slide deck is to accompany a presentation. The focus of the presentation is the speaker, and the slide deck is there merely as a visual aid.</p>
<p>Another reason to create a slide deck is to provide a lasting record of the presentation and to serve as a reference for the future. If the topic is &#8220;How to perform process X&#8221;, then you want to make sure that all of the necessary steps are spelled out on the slides, so that you can share a link to the slide deck in the invitation for others to refer to both during the presentation but mostly for reference afterwards. People will refer back to the slides the next time they need to do process X, and they probably don&#8217;t want to have to scrub through a one-hour recording just to extract the five steps they need to follow.</p>
<p>Yet another reason for creating a slide deck is when presenting to a senior executive, such as for use in a <a title="Microspeak: Placemat" href="https://devblogs.microsoft.com/oldnewthing/20200811-00/?p=104064"> placemat review</a>. There is probably a standard template for the meeting, such as an &#8220;Accomplishments since the last meeting&#8221;, perhaps broken into &#8220;What&#8217;s going well&#8221;, &#8220;What&#8217;s not going well&#8221;, and followed by an &#8220;Items of future concern&#8221;. The meeting may run into a time crunch (because <a title="There's no law that says a meeting can't end early" href="https://devblogs.microsoft.com/oldnewthing/20090819-00/?p=17053"> they always run over</a>), and you may not get enough time to get through all your slides, so you want to be able to say, &#8220;You can read the rest of the slides after the meeting.&#8221; (An extreme case of this is where other teams in the placemat meeting run long and leave you with no time at all!)</p>
<p>A related reason for creating a slide deck is as a &#8220;proof of work&#8221;. Going into the placement review, you don&#8217;t have much to report, but you have to fill the slide. If you don&#8217;t, it looks like you haven&#8217;t done anything.</p>
<p>I was reminded of this last reason when I worked with a colleague to prepare our team&#8217;s weekly placemat report. My colleague would fill the slide with information, and I would recommend replacing most of the bullet points with a much more succicient summary, or just removing the items outright.</p>
<blockquote class="q"><p>Partner Team X is considering the possibility of deploying a version that uses widgets before all of the widget features are fully implemented and rolled out to customers. They understand the consequences, such as impaired reverse widget flow. Some of these missing features will <a title="Microspeak: light up" href="https://devblogs.microsoft.com/oldnewthing/20150217-00/?p=44653"> light up</a> when the implementation is ready, but others will be stuck in a broken state unless explicit remediations are performed after the implementation rolls out, and a small number of features will be irreparably broken, with the only recourse being to destroy all the affected widgets and recreate them. Partner Team X has been advised of the various failure modes and how they can recover from the ones that are recoverable, and they are assessing whether they want to proceed with early deployment.</p></blockquote>
<p>I simplified it to</p>
<blockquote class="q"><p>Partner Team X understands the consequences of and limited recovery from using widgets in production before our work is fully implemented and deployed.</p></blockquote>
<p>The people who are receiving this slide deck are not interested in the fine details. They want the 25-word summary of how things are going so that they can either decide that our feature is not a point of concern, or decide to ask follow-up questions to understand the issue better. In that case, we can present a so-called <i>back-up slide</i> with the details.</p>
<p>A <i>back-up slide</i> is a slide in a slide deck that you hope that you won&#8217;t need to use, but which you create just in case it is needed. You mark it as hidden or put it in an appendix so that it doesn&#8217;t show up in the normal presentation flow. But if the audience you are presenting to (usually someone higher up the executive chain) asks a follow-up question about one of your slides, you can reach into your deck and pull out your back-up slide to answer it. &#8220;I was ready for that question, so I already have a slide with the data you are asking for.&#8221;</p>
<p><b>Bonus PowerPoint jargon</b>: <a title="Microspeak: PowerPoint Karaoke and the eye chart" href="https://devblogs.microsoft.com/oldnewthing/20110524-00/?p=10583/"> PowerPoint Karaoke and the eye chart</a>.</p>
<p>The post <a href="https://devblogs.microsoft.com/oldnewthing/20261020-00/?p=112784/">Microspeak: Drain the slide, back-up slides</a> appeared first on <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a>.</p>
]]></content:encoded>
    <wfw:commentRss>https://devblogs.microsoft.com/oldnewthing/20261020-00/?p=112784/feed/</wfw:commentRss>
    <slash:comments>0</slash:comments>
    <image type="image/png" url="https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2025/10/banner-oldnewthing-blue.webp"/>
  </item>
		
  <item>
    <title>Why do we need ud2 when int 6 is right there?</title>
    <link>https://devblogs.microsoft.com/oldnewthing/20261019-00/?p=112781/</link>
    <comments>https://devblogs.microsoft.com/oldnewthing/20261019-00/?p=112781/#comments</comments>
    <dc:creator><![CDATA[Raymond Chen]]></dc:creator>
    <pubDate>Mon, 19 Oct 2026 14:00:00 +0000</pubDate>
    <category><![CDATA[Old New Thing]]></category>
    <category><![CDATA[Code]]></category>
    <guid isPermaLink="false">https://devblogs.microsoft.com/oldnewthing/?p=112781</guid>
    <content:encoded><![CDATA[<p>Some time ago, I mentioned that <a title="Why is the x86 undefined instruction called ud2? Why 2?" href="https://devblogs.microsoft.com/oldnewthing/20260910-00/?p=112689"> the <code>ud2</code> instruction was created to be an architecturally undefined instruction</a>. But why not just use an <code>int 6</code> instruction? It&#8217;s a two-byte instruction, just like <code>ud2</code>, and interrupt vector 6 is the invalid opcode vector, so a program could just invoke it manually and get whatever behavior the operating system assigns to an attempt to execute an invalid opcode.</p>
<p>Well, not exactly.</p>
<p>Architecturally, the invalid opcode is a <i>fault</i>, which means that the address of the faulting instruction is reported to the exception handler. On the other hand, the <code>int 6</code> is a <i>trap</i>, which means that the address of the instruction <i>after</i> the <code>int 6</code> is reported to the handler.</p>
<p>This distinction between faults and traps reflects how they are used. A fault represents an error that the kernel could attempt to repair and retry. For example, a page fault could be repaired by paging the memory in. An access violation could be repaired by changing the page protections. A trap reports an operation that has already occurred and gives you a chance to do some special work in response to it before resuming execution. In the case of a software interrupt, the handler wants to process whatever the interrupt means, and then resume execution with the next instruction.</p>
<p>Now, some faults are unlikely to be repaired, but reporting them as faults means that you have to take extra steps if you want to execute past them. If you just ignore the fault, the instruction restarts and faults again.</p>
<p>If you manually raise the <code>int 6</code> interrupt to simulate a <tt>#UD</tt> undefined instruction fault, the handler will think it&#8217;s dealing with a CPU-generated undefined instruction fault, so it expects the instruction pointer in the fault frame to point to the undefined instruction. But since <code>int</code> puts the address of the <i>next</i> instruction into the fault frame, the fault handler will look at the instruction and get confused as to why the invalid instruction fault was raised. &#8220;It says here that the faulting instruction is a <code>xor eax, eax</code>. That&#8217;s a perfectly legitimate instruction. Why did it fault?&#8221;</p>
<p>One case for inspecting the faulting instruction is to emulate missing instructions on older processors.</p>
<p>Furthermore, in protected mode, you need to have permission to issue an <code>int 6</code>. The interrupt descriptor table describes what to do for each interrupt, as well as what CPU privilege level is permitted to invoke it explicitly. And to avoid exactly these types of problems where a user-mode program issues an <code>int 6</code> and confuses the <tt>#UD</tt> handler, all operating systems deny <code>int 6</code> privileges to user mode.</p>
<p>So even if you try to fake a <tt>#UD</tt> via an explicit <code>int 6</code>, it doesn&#8217;t work. To get the <tt>#UD</tt> undefined instruction fault, you have to issue an actual undefined instruction.</p>
<p>The post <a href="https://devblogs.microsoft.com/oldnewthing/20261019-00/?p=112781/">Why do we need &lt;CODE&gt;ud2&lt;/CODE&gt; when &lt;CODE&gt;int 6&lt;/CODE&gt; is right there?</a> appeared first on <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a>.</p>
]]></content:encoded>
    <wfw:commentRss>https://devblogs.microsoft.com/oldnewthing/20261019-00/?p=112781/feed/</wfw:commentRss>
    <slash:comments>9</slash:comments>
    <image type="image/png" url="https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2025/10/banner-oldnewthing-blue.webp"/>
  </item>
		
  <item>
    <title>C++ futures part 5: std::future and std::shared_future</title>
    <link>https://devblogs.microsoft.com/oldnewthing/20261016-00/?p=112778/</link>
    <comments>https://devblogs.microsoft.com/oldnewthing/20261016-00/?p=112778/#comments</comments>
    <dc:creator><![CDATA[Raymond Chen]]></dc:creator>
    <pubDate>Fri, 16 Oct 2026 14:00:00 +0000</pubDate>
    <category><![CDATA[Old New Thing]]></category>
    <category><![CDATA[Code]]></category>
    <guid isPermaLink="false">https://devblogs.microsoft.com/oldnewthing/?p=112778</guid>
    <content:encoded><![CDATA[<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>
<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>
<p>Forwarding the <code>get</code> to <code>consume</code> means that you can call <code>get</code> only once.</p>
<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>
<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>
<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>
<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>
<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>
<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>
<p><b>Bonus chatter</b>: All the names in the futures library are horrible. I mean, really, &#8220;packaged task&#8221;?</p>
<p>Here&#8217;s my attempt to give the pieces better names:</p>
<table style="border-collapse: collapse;" border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<th>Old name</th>
<th>New name</th>
</tr>
<tr>
<td>promise</td>
<td>promise_source</td>
</tr>
<tr>
<td>packaged_task</td>
<td>function_promise_source</td>
</tr>
<tr>
<td>async(deferred)</td>
<td>make_lazy_promise</td>
</tr>
<tr>
<td>async(async)</td>
<td>make_thread_promise</td>
</tr>
<tr>
<td>future</td>
<td>promise</td>
</tr>
<tr>
<td>shared_future</td>
<td>shared_promise</td>
</tr>
</tbody>
</table>
<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>
<p>The post <a href="https://devblogs.microsoft.com/oldnewthing/20261016-00/?p=112778/">C++ futures part 5: &lt;CODE&gt;std::&lt;WBR&gt;future&lt;/CODE&gt; and &lt;CODE&gt;std::&lt;WBR&gt;shared_future&lt;/CODE&gt;</a> appeared first on <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a>.</p>
]]></content:encoded>
    <wfw:commentRss>https://devblogs.microsoft.com/oldnewthing/20261016-00/?p=112778/feed/</wfw:commentRss>
    <slash:comments>2</slash:comments>
    <image type="image/png" url="https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2025/10/banner-oldnewthing-blue.webp"/>
  </item>
		
  <item>
    <title>C++ futures part 4: What if I want an immediate on-thread std::async?</title>
    <link>https://devblogs.microsoft.com/oldnewthing/20261015-00/?p=112776/</link>
    <comments>https://devblogs.microsoft.com/oldnewthing/20261015-00/?p=112776/#comments</comments>
    <dc:creator><![CDATA[Raymond Chen]]></dc:creator>
    <pubDate>Thu, 15 Oct 2026 14:00:00 +0000</pubDate>
    <category><![CDATA[Old New Thing]]></category>
    <category><![CDATA[Code]]></category>
    <guid isPermaLink="false">https://devblogs.microsoft.com/oldnewthing/?p=112776</guid>
    <content:encoded><![CDATA[<p><a title="C++ futures part 3: The mental model for std::async" href="https://devblogs.microsoft.com/oldnewthing/20261014-00/?p=112774"> I noted last time</a> that you are not allowed to ask <code>std::<wbr />async</code> for an immediate on-thread calculation. The C++ library designers probably chose not to support it because there&#8217;s no point: An immediate on-thread calculation is just a function call, so call the function already and leave me out of it.</p>
<p>But maybe you want to do it because you are interfacing with a component that wants a <code>std::<wbr />future</code>. How could you do it?</p>
<p>One way would be to implement the immediately synchronous case manually by setting a value into a promise.</p>
<pre>template&lt;typename F, typename... Args&gt;
auto
    make_immediate_future(F&amp;&amp; f, Args&amp;&amp;... args)
-&gt; std::future&lt;
        std::invoke_result_t&lt;std::decay_t&lt;F&gt;,
                             std::decay_t&lt;Args&gt;...&gt;&gt;
{
    std::promise&lt;
        std::invoke_result_t&lt;std::decay_t&lt;F&gt;,
                             std::decay_t&lt;Args&gt;...&gt;&gt; p;
    try {
        p.set_value(std::invoke(auto(f), auto(args)...));
    } catch (...) {
        p.set_exception(std::current_exception());
    }
    return p.get_future();
}
</pre>
<p>Or you could make <code>packaged_task</code> do the work for you:</p>
<pre>template&lt;typename F, typename... Args&gt;
auto
    make_immediate_future(F&amp;&amp; f, Args&amp;&amp;... args)
-&gt; std::future&lt;
        std::invoke_result_t&lt;std::decay_t&lt;F&gt;,
                             std::decay_t&lt;Args&gt;...&gt;&gt;
{
    using R = std::invoke_result_t&lt;std::decay_t&lt;F&gt;,
                                   std::decay_t&lt;Args&gt;...&gt;;

    std::packaged_task&lt;R(Args...)&gt; task(f);
    task(args...);
    return task.get_future();
}
</pre>
<p>Yet another way would be to ask for an on-demand in-thread execution, and then immediately demand it.</p>
<pre>template&lt;typename F, typename... Args&gt;
auto
    make_immediate_future(F&amp;&amp; f, Args&amp;&amp;... args)
-&gt; std::future&lt;
        std::invoke_result_t&lt;std::decay_t&lt;F&gt;,
                             std::decay_t&lt;Args&gt;...&gt;&gt;
{
    auto f = std::async(std::launch::deferred,
                        std::forward&lt;F&gt;(f),
                        std::forward&lt;Args&gt;(args)...);
    f.wait();
    return f;
}
</pre>
<p>The post <a href="https://devblogs.microsoft.com/oldnewthing/20261015-00/?p=112776/">C++ futures part 4: What if I want an immediate on-thread &lt;CODE&gt;std::async&lt;/CODE&gt;?</a> appeared first on <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a>.</p>
]]></content:encoded>
    <wfw:commentRss>https://devblogs.microsoft.com/oldnewthing/20261015-00/?p=112776/feed/</wfw:commentRss>
    <slash:comments>1</slash:comments>
    <image type="image/png" url="https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2025/10/banner-oldnewthing-blue.webp"/>
  </item>
		
  <item>
    <title>C++ futures part 3: The mental model for std::async</title>
    <link>https://devblogs.microsoft.com/oldnewthing/20261014-00/?p=112774/</link>
    <comments>https://devblogs.microsoft.com/oldnewthing/20261014-00/?p=112774/#comments</comments>
    <dc:creator><![CDATA[Raymond Chen]]></dc:creator>
    <pubDate>Wed, 14 Oct 2026 14:00:00 +0000</pubDate>
    <category><![CDATA[Old New Thing]]></category>
    <category><![CDATA[Code]]></category>
    <guid isPermaLink="false">https://devblogs.microsoft.com/oldnewthing/?p=112774</guid>
    <content:encoded><![CDATA[<p><a title="C++ futures part 2: The mental model for std::promise, std::packaged_task" href="https://devblogs.microsoft.com/oldnewthing/20261013-00/?p=112769"> We continue our study of C++ futures</a> with <code>std::<wbr />async</code>.</p>
<p>The more complex case is the <code>std::<wbr />async</code>. You give <code>std::<wbr />async</code> a callable and optional parameters to pass to that callable, and you also specify how and when you want the callable to be invoked. You get two axes: Do you want it to be calculated immediately or on demand? Do you want the calculation to happen on the thread that requests the value or on its own thread?</p>
<p>Now, you&#8217;d think you could fill out the table like this:</p>
<table style="border-collapse: collapse;" border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<th> </th>
<th>Immediate</th>
<th>On demand (&#8220;deferred&#8221;)</th>
</tr>
<tr>
<th>Same thread</th>
<td>Call now and save the value</td>
<td>Call when waited</td>
</tr>
<tr>
<th>New thread (&#8220;async&#8221;)</th>
<td>Start the thread now</td>
<td>Start the thread when waited</td>
</tr>
</tbody>
</table>
<p>However, that&#8217;s not what the C++ standard says.</p>
<p>The C++ standard disallows asking for an immediate value on the same thread. There&#8217;s really no value to doing it that way, since there is no actual &#8220;future&#8221;; it all happens in the present! &#8220;This future could have been a function call.&#8221;</p>
<pre>auto f = std::async({}, calculator);

auto result = f.get();
</pre>
<p>This would, in theory, mean &#8220;Immediately call the calculator and wait for it to produce a value. Store it in the <code>source</code> and return a future that fetches the value from that source.&#8221; But that&#8217;s the same as just</p>
<pre>auto result = calculator();
</pre>
<p>I&#8217;m guessing that the C++ standard disallows it because there&#8217;s no point.</p>
<p>The C++ standard also says that if you combine both <code>std::<wbr />launch::<wbr />deferred</code> and <code>std::<wbr />launch::<wbr />async</code>, it doesn&#8217;t mean that you get a calculation that lazy-starts on its own thread. Instead, the standard says that the implementation can choose which flag to honor and which to ignore.</p>
<p>So really, the table is</p>
<table style="border-collapse: collapse;" border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<th> </th>
<th>Immediate</th>
<th>On demand (&#8220;deferred&#8221;)</th>
</tr>
<tr>
<th>Same thread</th>
<td>Illegal</td>
<td>Call when waited</td>
</tr>
<tr>
<th>New thread (&#8220;async&#8221;)</th>
<td>Start the thread now</td>
<td>Dealer&#8217;s choice</td>
</tr>
</tbody>
</table>
<p>In all cases, the result of the callable (either a value or an exception) is stored in the <code>source</code>.</p>
<p>Now, I noted that there&#8217;s no point to asking <code>std::<wbr />async</code> for an immediate on-thread calculation. But maybe you want to do it anyway because you are interfacing with some other component that expects to receive a <code>future</code>. How could you get the same result?</p>
<p>We&#8217;ll look at that next time.</p>
<p>The post <a href="https://devblogs.microsoft.com/oldnewthing/20261014-00/?p=112774/">C++ futures part 3: The mental model for &lt;CODE&gt;std::async&lt;/CODE&gt;</a> appeared first on <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a>.</p>
]]></content:encoded>
    <wfw:commentRss>https://devblogs.microsoft.com/oldnewthing/20261014-00/?p=112774/feed/</wfw:commentRss>
    <slash:comments>1</slash:comments>
    <image type="image/png" url="https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2025/10/banner-oldnewthing-blue.webp"/>
  </item>
		
  <item>
    <title>C++ futures part 2: The mental model for std::promise, std::packaged_task</title>
    <link>https://devblogs.microsoft.com/oldnewthing/20261013-00/?p=112769/</link>
    <comments>https://devblogs.microsoft.com/oldnewthing/20261013-00/?p=112769/#comments</comments>
    <dc:creator><![CDATA[Raymond Chen]]></dc:creator>
    <pubDate>Tue, 13 Oct 2026 14:00:00 +0000</pubDate>
    <category><![CDATA[Old New Thing]]></category>
    <category><![CDATA[Code]]></category>
    <guid isPermaLink="false">https://devblogs.microsoft.com/oldnewthing/?p=112769</guid>
    <content:encoded><![CDATA[<p><a title="C++ futures part 1: The mental model" href="https://devblogs.microsoft.com/oldnewthing/20261012-00/?p=112767"> Last time</a>, we looked at the &#8220;state&#8221; that powers the <code>std::promise</code>, <code>std::packaged_task</code>, <code>std::async</code>, <code>std::future</code>, and <code>std::shared<wbr />future</code> classes. Now we&#8217;ll see how that &#8220;state&#8221; (which I call the <code>source</code>) gets used and passed around.</p>
<p>The lifetime of the <code>source</code> is managed by a <code>std::<wbr />shared_<wbr />ptr&lt;source&lt;T&gt;&gt;</code>. Everybody who has a reference to the source uses this shared pointer.</p>
<p>The simplest case is <code>std::promise</code>. The operations on <code>std::promise</code> pretty much map directly to the operations on the <code>source</code> we laid out last time.</p>
<p>The next more complicated case is the <code>std::<wbr />packaged_task</code>. This takes a callable and saves it for later. You can then call <code>get_future</code> to get a future and hand it to somebody as an IOU for &#8220;A value will appear here someday.&#8221;</p>
<p>When you want to produce a value, you invoke the <code>packaged_task</code> with whatever parameters you want to pass to the original callable. The result of the invocation (either a value or an exception) is then stored in the <code>source</code> object, which can then be retrieved by the future.</p>
<p>You can imagine a packaged task as just wrapping a promise:</p>
<pre>template&lt;typename R, typename... Args&gt;
class packaged_task
{
    std::promise&lt;R&gt; p;
    std::function&lt;R(Args...)&gt; func;

public:
    template&lt;typename F&gt;
    packaged_task(F&amp;&amp; f) : func(std::forward&lt;F&gt;(f)) {}

    auto get_future() { return p.get_future(); }

    void operator()(Args... args) {
        try {
            p.set_value(func(args...));
        } catch (...) {
            p.set_exception(std::current_exception());
        }
    }
};
</pre>
<p>When you invoke it, it calls the stored callable with the arguments and puts the result in the promise.</p>
<p>Okay, those were the easy cases of pushing values into the <code>source</code>. Next time, we&#8217;ll look at the messy case: <code>std::<wbr />async</code>.</p>
<p>The post <a href="https://devblogs.microsoft.com/oldnewthing/20261013-00/?p=112769/">C++ futures part 2: The mental model for &lt;CODE&gt;std::promise&lt;/CODE&gt;, &lt;CODE&gt;std::packaged_task&lt;/CODE&gt;</a> appeared first on <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a>.</p>
]]></content:encoded>
    <wfw:commentRss>https://devblogs.microsoft.com/oldnewthing/20261013-00/?p=112769/feed/</wfw:commentRss>
    <slash:comments>3</slash:comments>
    <image type="image/png" url="https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2025/10/banner-oldnewthing-blue.webp"/>
  </item>
		
  <item>
    <title>C++ futures part 1: The mental model</title>
    <link>https://devblogs.microsoft.com/oldnewthing/20261012-00/?p=112767/</link>
    <comments>https://devblogs.microsoft.com/oldnewthing/20261012-00/?p=112767/#comments</comments>
    <dc:creator><![CDATA[Raymond Chen]]></dc:creator>
    <pubDate>Mon, 12 Oct 2026 14:00:00 +0000</pubDate>
    <category><![CDATA[Old New Thing]]></category>
    <category><![CDATA[Code]]></category>
    <guid isPermaLink="false">https://devblogs.microsoft.com/oldnewthing/?p=112767</guid>
    <content:encoded><![CDATA[<p>The C++ standard library has a group of related functions and classes: <code>std::promise</code>, <code>std::packaged_task</code>, <code>std::async</code>, <code>std::future</code>, and <code>std::shared<wbr />future</code>.</p>
<p>The idea is that all of these classes are built of a single unnamed class which the C++ standard library calls the &#8220;state&#8221;, but for the purpose of discussion, I&#8217;m going to call it the <code>source</code>, since it is the thing that produces the value, and it behaves analogously to a C# task completion source.</p>
<p>The <code>source</code> conceptually supports these operations:</p>
<ul>
<li>Set a value.</li>
<li>Set an exception.</li>
<li>Wait for a value or exception to be set.</li>
<li>Retrieve the value or rethrow the exception.</li>
</ul>
<p>Here&#8217;s a simplified version that doesn&#8217;t support references.</p>
<pre>template&lt;typename T&gt;
struct source
{
private:
    enum what {
        nothing,
        value,
        exception,
        consumed,
    } d; // discriminant

    union {
        T value;
        std::exception_ptr ex;
    } r; // result

    std::mutex m;
    std::condition_variable cv;
</pre>
<p>The discriminant tells us what is in the union: Nothing (because nobody has set anything), a value, an exception, or nothing (because the value has been consumed).</p>
<p>The mutex ensures thread-safe access to the other members, and the condition variable lets us wait for somebody to set a value or exception.</p>
<pre>    void must_be_empty() {
        if (d != what::empty) {
            throw std::future_error(future_errc::promise_already_satisfied);
        }
    }
</pre>
<p>You can call the <code>set</code> methods only when the source is empty, so we&#8217;ll factor out the emptiness check.</p>
<pre>public:
    void set_value(T&amp; v) {
        std::lock_guard g(m);
        must_be_empty();
        new(&amp;r.value) T(v); // "r.value = v"
        d = what::value;
        cv.notify_all();
    }
</pre>
<p>You can set a value or exception only once, so if there is already a value or exception, we yell at you. Otherwise, we save the value and notify anybody who was waiting for a value to be set.</p>
<p>I won&#8217;t bother with the overload that takes <code>T&amp;&amp;</code>; it&#8217;s the same thing, but with a <code>std::move()</code>.</p>
<p>There is also a <code>set_<wbr />value_<wbr />at_<wbr />thread_<wbr />exit</code> which will be another topic for another day.</p>
<p>Instead of setting a value, you can set an exception:</p>
<pre>    void set_exception(std::exception_ptr ex) {
        std::lock_guard g(m);
        must_be_empty();
        new(&amp;r.ex) std::exception_ptr(ex); // "r.ex = ex"
        d = what::exception;
        cv.notify_all();
    }
</pre>
<p>It&#8217;s basically the same as <code>set_<wbr />value</code> except that we store an exception.</p>
<p>That&#8217;s how values an exceptions go <i>into</i> the source. The next part is waiting for the source to become nonempty.</p>
<pre>    void wait() {
        std::lock_guard g(m);
        while (d == what::empty) {
            cv.wait(g);
        }
    }

    void wait_for(duration dur) {
        std::lock_guard g(m);
        cv.wait_for(g, dur, [&amp;] { return d != what::empty; });
    }
</pre>
<p>You can either wait indefinitely or wait for a particular duration. The <code>wait_for</code> method is why we have a condition variable at all. If it weren&#8217;t for that guy, we could have used <code>std::<wbr />atomic</code>&#8216;s <code>wait</code> and <code>notify_<wbr />all</code> methods.</p>
<p>The last part is getting the values out.</p>
<pre>    void can_get() {
        if (d == what::consumed) {
            throw std::future_error(future_errc::future_already_retrieved);
        }
        if (d == what::exception) {
            std::rethrow_exception(r.ex);
        }
    }
</pre>
<p>Regardless of how we intend to retrieve the value, it&#8217;s not legal to retrieve a value that has already been consumed. Also, if the result is an exception, we rethrow the exception.</p>
<pre>    T consume() {
        wait();
        std::lock_guard g(m);
        can_get();
        d = what::consumed;
        return std::move(r.v);
    }

    const T&amp; get() {
        wait();
        std::lock_guard g(m);
        can_get();
        return r.v;
    }
};
</pre>
<p>You can get the value, which returns a const reference to the underlying storage. Or you can consume the value, which moves the value out of the underlying storage and marks the value as consumed so nobody can consume or get it (because it&#8217;s not there any more).</p>
<p>We can now build our other objects and functions out of this guy. We&#8217;ll do that next time.</p>
<p><b>Bonus chatter</b>: Writing the destructor is left as an exercise for the reader. It&#8217;s not hard, but it&#8217;s annoying because of the union.</p>
<p><b>Bonus bonus chatter</b>: I&#8217;m writing the series because I always forget how it works, but writing it down helps me remember.</p>
<p>The post <a href="https://devblogs.microsoft.com/oldnewthing/20261012-00/?p=112767/">C++ futures part 1: The mental model</a> appeared first on <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a>.</p>
]]></content:encoded>
    <wfw:commentRss>https://devblogs.microsoft.com/oldnewthing/20261012-00/?p=112767/feed/</wfw:commentRss>
    <slash:comments>6</slash:comments>
    <image type="image/png" url="https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2025/10/banner-oldnewthing-blue.webp"/>
  </item>
		
  <item>
    <title>One way of thinking about access specifiers in C++/WinRT implementation types</title>
    <link>https://devblogs.microsoft.com/oldnewthing/20261009-00/?p=112764/</link>
    <comments>https://devblogs.microsoft.com/oldnewthing/20261009-00/?p=112764/#respond</comments>
    <dc:creator><![CDATA[Raymond Chen]]></dc:creator>
    <pubDate>Fri, 09 Oct 2026 14:00:00 +0000</pubDate>
    <category><![CDATA[Old New Thing]]></category>
    <category><![CDATA[Code]]></category>
    <guid isPermaLink="false">https://devblogs.microsoft.com/oldnewthing/?p=112764</guid>
    <content:encoded><![CDATA[<p>In C++/WinRT, Windows Runtime classes are exposed to the outside world through the projection, but internally they are represented by their implementation type. Since the outside world sees only the projection, implementation types are visible only to the module that produced them.</p>
<p>This realization lets you reinterpret the C++ access specifiers for C++/WinRT implementation types.</p>
<table class="cp3" style="border-collapse: collapse;" border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<th>Declared on<br />
implementation as</th>
<th>C# and C++/CX<br />
equivalent</th>
<th>Accessible to</th>
</tr>
<tr>
<td>implemented interface</td>
<td><tt>public</tt></td>
<td>Anyone</td>
</tr>
<tr>
<td><tt>public</tt></td>
<td><tt>internal</tt></td>
<td>Code in the same module</td>
</tr>
<tr>
<td><tt>protected</tt></td>
<td><tt>protected</tt></td>
<td>Code in derived classes</td>
</tr>
<tr>
<td><tt>private</tt></td>
<td><tt>private</tt></td>
<td>Code in the same class</td>
</tr>
</tbody>
</table>
<p>In code:</p>
<pre>struct Widget : winrt::implements&lt;Widget, IStringable&gt;
{
public:
    // This is a member of IStringable,
    // so it is accessible to anyone.
    winrt::hstring ToString();

    // This does not implement any member from IStringable,
    // so it is accessible to other code in the same module.
    void Recolor(winrt::Windows::UI::Color newColor);

protected:
    // Accessible to implementation classes that derive from Widget.
    void Reverse();

private:
    // Accessible only to Widget itself.
    void Reset();
};
</pre>
<p>Note that if you are implementing a runtime class, then C++/WinRT autogenerates a template for you that auto-declares all of the declared interfaces of that class.</p>
<pre>// Widget.idl

namespace Contoso
{
    runtimeclass Widget : Windows.Foundation.IStringable
    {
        String Name{ get; };
        void Toggle();
    }
}
</pre>
<p>The IDL compiler autogenerates an interface <code>IWidget</code> with a property <code>Name</code> and a method <code>Toggle()</code>.</p>
<pre>// implementation
namespace winrt::Contoso
{
    struct Widget : <span style="border: solid 1px currentcolor;">WidgetT&lt;Widget&gt;</span>
    {
        ⟦ ... ⟧
    };
}
</pre>
<p>The <code>WidgetT&lt;Widget&gt;</code> expands to <code>implements&lt;Widget, IWidget, Windows::Foundation::IStringable&gt;</code> because those are the interfaces declared by the IDL file.</p>
<p>The post <a href="https://devblogs.microsoft.com/oldnewthing/20261009-00/?p=112764/">One way of thinking about access specifiers in C++/WinRT implementation types</a> appeared first on <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a>.</p>
]]></content:encoded>
    <wfw:commentRss>https://devblogs.microsoft.com/oldnewthing/20261009-00/?p=112764/feed/</wfw:commentRss>
    <slash:comments>0</slash:comments>
    <image type="image/png" url="https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2025/10/banner-oldnewthing-blue.webp"/>
  </item>
		
  <item>
    <title>If one anti-malware software is good, does that make two better?</title>
    <link>https://devblogs.microsoft.com/oldnewthing/20261008-00/?p=112762/</link>
    <comments>https://devblogs.microsoft.com/oldnewthing/20261008-00/?p=112762/#comments</comments>
    <dc:creator><![CDATA[Raymond Chen]]></dc:creator>
    <pubDate>Thu, 08 Oct 2026 14:00:00 +0000</pubDate>
    <category><![CDATA[Old New Thing]]></category>
    <category><![CDATA[Tips/Support]]></category>
    <guid isPermaLink="false">https://devblogs.microsoft.com/oldnewthing/?p=112762</guid>
    <content:encoded><![CDATA[<p>A colleague on the enterprise support team was investigating a complex failure from a customer, and after much analysis, it appeared that the problem was that the system had two different anti-malware programs installed. Let&#8217;s call then Contoso and Fabrikam. Fabrikam called a function that Contoso had detoured. Contoso&#8217;s detour thought this was suspicious, so it tried to quarantine the Fabrikam process. Unfortunately, Fabrikam had also detoured a function that Contoso was using. The result was mass confusion as the Contoso ended up calling into something that it was trying to quarantine.</p>
<p>Installing two anti-malware programs on the same system is like hiring two different security companies to patrol your building. If they don&#8217;t know about each other, each is going to think the other one is an intruder. If you&#8217;re lucky, their instructions are merely to report on suspicious activity.</p>
<p>But if you give them weapons and the authority to use them, you may end up with your two security companies pointing their weapons at each other.</p>
<p>In real life, you would introduce the two security companies to each other, or at least make sure they don&#8217;t patrol the same floor.</p>
<p>In software, this is harder to do. It&#8217;s not like you can invite two programs to lunch and have them get to know each other.</p>
<p>Anti-malware software typically does things that aren&#8217;t officially supported, like detouring system functions. And these detours mean that calling a system function no longer does the system thing; instead, it calls into the anti-malware software, and it might decide to do something unrelated to the system function you thought were calling, and that unrelated thing may itself start causing problems, say, by hanging. Not only is there no easy way to identify that this has happened, short of debugging an actual malfunctioning system, but even after you figure out the conflict, there&#8217;s usually no way to tell each anti-malware software to &#8220;stay on its floor.&#8221;</p>
<p>The post <a href="https://devblogs.microsoft.com/oldnewthing/20261008-00/?p=112762/">If one anti-malware software is good, does that make two better?</a> appeared first on <a href="https://devblogs.microsoft.com/oldnewthing">The Old New Thing</a>.</p>
]]></content:encoded>
    <wfw:commentRss>https://devblogs.microsoft.com/oldnewthing/20261008-00/?p=112762/feed/</wfw:commentRss>
    <slash:comments>12</slash:comments>
    <image type="image/png" url="https://devblogs.microsoft.com/oldnewthing/wp-content/uploads/sites/38/2025/10/banner-oldnewthing-blue.webp"/>
  </item>
	</channel>
</rss>
