title: How to wait for completion of all boost:asio's stackful coroutines?
body: <p>I'm starting a number of coroutines with asio::spawn, and I want to wait until all of then are finished and do some other work then. How it can be done?</p>
  
  <p>The control flow is following:</p>
  
  <pre><code>asio::spawn (io, [] (asio::yield_context yield) {
    ...
    // starting few coroutines
    asio::spawn (yield, [] (asio::yield_context yield2) { ... });
    asio::spawn (yield, [] (asio::yield_context yield2) { ... });
    asio::spawn (yield, [] (asio::yield_context yield2) { ... });
    asio::spawn (yield, [] (asio::yield_context yield2) { ... });
  
    // now I want to wait for all of them to finish before I do 
    // some other work?
    ...
  });
  
  io.run ();
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Below is the sample code </p>
  
  <pre><code>#include &lt;boost/asio.hpp&gt;
  #include &lt;boost/asio/spawn.hpp&gt;
  #include &lt;boost/asio/steady_timer.hpp&gt;
  #include &lt;chrono&gt;
  
  #include &lt;iostream&gt;
  using namespace std;
  
  int main ()
  {
    using namespace boost::asio;
  
    io_service io;
  
    spawn (io, [&amp;] (yield_context yield) {
      cout &lt;&lt; "main coro starts\n";
  
      auto lambda = [&amp;] (yield_context yield)
        {
          cout &lt;&lt; "in lambda inside subcoroutine - starts\n";
          steady_timer t (io, std::chrono::seconds (1));
          t.async_wait (yield);
          cout &lt;&lt; "in lambda inside subcoroutine - finishes\n";
        };
  
      // starting few coroutines
      spawn (yield, lambda);
      spawn (yield, lambda);
  
      // now I want to wait for all of them to finish before I do
      // some other work?
      // ???
  
      cout &lt;&lt; "main coro finishes\n";
    });
  
    io.run ();
  }
  </code></pre>
  
  <p>And the output is: </p>
  
  <pre><code>// main coro starts
  // in lambda inside subcoroutine - starts
  // in lambda inside subcoroutine - starts
  // main coro finishes &lt;----
  // in lambda inside subcoroutine - finishes
  // in lambda inside subcoroutine - finishes
  </code></pre>
  
  <p>While I'm expecting:</p>
  
  <pre><code>// main coro starts
  // in lambda inside subcoroutine - starts
  // in lambda inside subcoroutine - starts
  // in lambda inside subcoroutine - finishes
  // in lambda inside subcoroutine - finishes
  // main coro finishes
  </code></pre>
  
  <p>(see the place of "main coro finishes" line)</p>