title: How can I modify the std::launch policy afterwards for std::async?
body: <p>Suppose I want to use <code>std::async</code> for parallelism in my c++ code to run a computationally heavy function <code>func</code>. Now since it is a heavy function, we might at first use <code>std::launch::deferred</code> policy for it as the situation is that we may not need to run after all.</p>
  
  <p>But again if ever we need to execute them <em>suddenly</em> in the future, we desire to run in parallel. Then how can we modify the <code>std::launch</code> policy afterwards.</p>
  
  <p>[Well, one can say that why don't you then <em>suddenly</em> create the <code>std::async</code>s as <em>suddenly</em> you need to execute. But I am assuming here that I cannot do that.] </p>
  
  <p>Or, is there any better and cleaner way to do this other than using <code>std::async</code>? </p>
  
  <p>Any help is highly appreciated. Thanks in advance.</p>
  
  <pre><code>#include &lt;future&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  
  std::vector&lt;double&gt; func(size_t n) // a computationally heavy function
  {
      std::vector&lt;double&gt; vec(n);
      std::generate_n(vec.begin(), n, std::rand);
      return vec;
  }
  
  int main()
  {
      // create asyncs, now deferred for lazy execution
      auto v1 = std::async(std::launch::deferred, func, 200); // deferred for lazy execution
      auto v2 = std::async(std::launch::deferred, func, 250); // deferred for lazy execution
  
      // only after sometime we decide to execute both of them
      // but we also now want them to execute in parallel
  
      // so how can we now change the launch policy?
  
      // to get the values as quickly as can be
      auto n1 = v1.get().size();
      auto n2 = v2.get().size();
  
      std::cout&lt;&lt;"Got "&lt;&lt;n1&lt;&lt;" and "&lt;&lt;n2&lt;&lt;" random numbers in parallel!"&lt;&lt;std::endl;
      return 0;
  }
  </code></pre>
  
  <h2>UPDATE</h2>
  
  <p>Thinking a little bit more leads me with this issue:</p>
  
  <p>After defining the <code>std::async</code> with <code>std::launch::deferred</code>, when one calls the <code>.get()</code> function, will it then be <em>guaranteed</em> to run <em>async</em> (i.e. in parallel) Surely, not. <a href="http://en.cppreference.com/w/cpp/thread/launch" rel="nofollow noreferrer">http://en.cppreference.com/w/cpp/thread/launch</a> says </p>
  
  <blockquote>
    <p>it is executed on the calling thread.</p>
  </blockquote>
  
  <p>Then the very notion of <em>async</em> gets spoilt, right?</p>