title: C++ async + future (deferred vs async)
body: <p>This is the test program that I am working with.  Can someone please describe in detail what is happening and the reason for this output?</p>
  <p>Why does <code>launch::async</code> get the value of <code>g_num</code> as <code>0</code>, while <code>launch::deferred</code> gets <code>100</code>?</p>
  <p>Both <code>launch::async</code> and <code>launch::deferred</code> got correct values of <code>arg</code> that is on the <code>main</code> stack, which I believe means they should have both gotten <code>100</code>.</p>
  <h2>The code:</h2>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;future&gt;
  using namespace std;
      
  thread_local int g_num;
      
  int read_it(int x) {
      return g_num + x;
  }
  
  int main()
  {
      g_num = 100;
  
      int arg = 1;
      future&lt;int&gt; fut = async(launch::deferred, read_it, arg);
      arg = 2;
      future&lt;int&gt; fut2 = async(launch::async, read_it, arg);
  
      cout &lt;&lt; &quot;Defer: &quot; &lt;&lt; fut.get() &lt;&lt; endl;
      cout &lt;&lt; &quot;Async: &quot; &lt;&lt; fut2.get() &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  <h2>The output:</h2>
  <pre><code>Defer: 101
  Async: 2
  </code></pre>