title: With C++ std::async, why do we need packaged_task and promise?
body: <p>I'm learning about C++ thread library, and I feel the async, packaged_task and promise are doing things that are pretty similar: they all run a function asynchronously in a separate thread, and return a future object that stores the function's return value. So I do not quite understand why we need all three of them, instead of always using async. </p>
  
  <p>Below is an example code from <a href="https://en.cppreference.com/w/cpp/thread/future" rel="nofollow noreferrer">std::future documentation</a>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;future&gt;
  #include &lt;thread&gt;
  
  int main()
  {
      // future from a packaged_task
      std::packaged_task&lt;int()&gt; task([]{ return 7; }); // wrap the function
      std::future&lt;int&gt; f1 = task.get_future();  // get a future
      std::thread t(std::move(task)); // launch on a thread
  
      // future from an async()
      std::future&lt;int&gt; f2 = std::async(std::launch::async, []{ return 8; });
  
      // future from a promise
      std::promise&lt;int&gt; p;
      std::future&lt;int&gt; f3 = p.get_future();
      std::thread( [&amp;p]{ p.set_value_at_thread_exit(9); }).detach();
  
      std::cout &lt;&lt; "Waiting..." &lt;&lt; std::flush;
      f1.wait();
      f2.wait();
      f3.wait();
      std::cout &lt;&lt; "Done!\nResults are: "
            &lt;&lt; f1.get() &lt;&lt; ' ' &lt;&lt; f2.get() &lt;&lt; ' ' &lt;&lt; f3.get() &lt;&lt; '\n';
      t.join();
  }  
  </code></pre>
  
  <p>and this example at least shows under certain circumstances, std::async can replace the other two. Can someone give an example that I have to use promise or packaged_task where the std::async is not enough to do the job?</p>