title: boost:asio thread pool implementation for occasionally synchronized tasks
body: <p>I have a "main" function that performs many small, independent tasks each once per time step. However, after each time step, I must wait for all of the tasks to complete before stepping forward.</p>
  
  <p>I want to make the program multithreaded. I have tried implementations with the boost-offshoot threadpool, and I've tried using a vector of (shared pointers to) threads, and I've tried the asio threadpool ideas (using an io_service, establishing some work, then distributing run to the threads and posting handlers to the io_service). </p>
  
  <p>All of these seem to have a lot of overhead creating and destroying threads for my "many small tasks," and I want a way, preferably using the asio tools, to instantiate one io_service, one thread_group, posting handlers to the io_service, and waiting for a single time step's work to be finished before posting more tasks. Is there a good way to do this? Here's (stripped down) code for what I have working now:</p>
  
  <pre><code>boost::asio::io_service io_service;
  for(int theTime = 0; theTime != totalTime; ++theTime)
  {
      io_service.reset();
      boost::thread_group threads;
      // scoping to destroy the work object after work is finished being assigned
      {
          boost::asio::io_service::work work(io_service);
          for (int i = 0; i &lt; maxNumThreads; ++i)
          {
              threads.create_thread(boost::bind(&amp;boost::asio::io_service::run,
                  &amp;io_service));
          }
  
          for(int i = 0; i &lt; numSmallTasks; ++i)
          {
              io_service.post(boost::bind(&amp;process_data, i, theTime));
          }
      }
      threads.join_all(); 
  }
  </code></pre>
  
  <p>Here's what I had rather have (but don't know how to implement):</p>
  
  <pre><code>boost::asio::io_service io_service;
  boost::thread_group threads;
  boost::asio::io_service::work work(io_service);
  for (int i = 0; i &lt; maxNumThreads; ++i)
  {
      threads.create_thread(boost::bind(&amp;boost::asio::io_service::run,
           &amp;io_service));
  }
  
  for(int theTime = 0; theTime != totalTime; ++theTime)
  {
      for(int i = 0; i &lt; numSmallTasks; ++i)
      {
          io_service.post(boost::bind(&amp;process_data, i, theTime));
      }
      // wait here until all of these tasks are finished before looping 
      // **** how do I do this? *****
  }
  // destroy work later and join all threads later...
  </code></pre>