title: boost io_service non-blocking parallel execution?
body: <p>I have run into a dilemma whilst using boost::asio and boost::io_service</p>
  
  <p>My classes wrap around the <a href="http://www.boost.org/doc/libs/1_41_0/doc/html/boost_asio/example/http/client/async_client.cpp" rel="nofollow">async client example</a> provided by boost for socket connections.
  I use another class which encapsulates:</p>
  
  <pre><code>class service_controller
  {
      ...
  
      /// IO service
      boost::asio::io_service __io_service;
  
      /// Endpoint Resolver
      boost::asio::ip::tcp::resolver::query __query;
  
      /// Resolution for TCP
      boost::asio::ip::tcp::resolver __resolver;
  }
  </code></pre>
  
  <p>So, when I construct my clients, the constructor takes references:</p>
  
  <pre><code>asio_service_client (
                          boost::asio::ip::tcp::resolver::query &amp; query,
                          boost::asio::ip::tcp::resolver &amp; resolver,
                          boost::asio::io_service &amp; io_service
                      );
  </code></pre>
  
  <p>Everything works fine, but I have to call </p>
  
  <blockquote>
    <p>io_service.run()</p>
  </blockquote>
  
  <p>At the end, after creating all all my clients.
  If I encapsulate seperate io_service objects for each client, I essentially remove the async io nature, as each one will block until its finished.
  Therefore, I decided to form a type of group, by making all client objects use the same io_service.</p>
  
  <p>io_service::poll() does not appear to work at all (nothing happens), nor does io_service::run_one().</p>
  
  <p>In fact, the only thing that appears to work, is:</p>
  
  <pre><code>    // with a callback =&gt; the callback will run once finished
      rapp::services::asio_service_client c1( ctrl.Query(), ctrl.Resolver(), ctrl.Services() );
  
      // without a callback =&gt; asio_service_client::handle_reply will run once finished
      rapp::services::asio_service_client c2 ( ctrl.Query(), ctrl.Resolver(), ctrl.Services() );
      rapp::services::asio_service_client c3 ( ctrl.Query(), ctrl.Resolver(), ctrl.Services() );
      rapp::services::asio_service_client c4 ( ctrl.Query(), ctrl.Resolver(), ctrl.Services() );
  
      // Run services c1, c2
      c1.Run( header, post,
              [&amp;]( boost::asio::streambuf &amp; buffer )
              { 
                  std::string raw ( ( std::istreambuf_iterator&lt;char&gt;( &amp;buffer ) ), std::istreambuf_iterator&lt;char&gt;() );
                  std::cout &lt;&lt; raw &lt;&lt; std::endl;
              });
  
      c2.Run( header, post );
      ctrl.Services().run();
  
      /// Run remaining services ( c3, c4 )
      c3.Run( header, post );
      c4.Run( header, post );
  
      ctrl.Services().reset();
      ctrl.Services().run();
  </code></pre>
  
  <p>Unless of course, if I request a group to be run altogether (e.g., ask for c1, c2, c3 and c4 Run).</p>
  
  <p>Is there some way, or some class pattern, where I could automate a queue, where I create objects, add them, and they are run asynchronously? Ideally with threads, but without will also work.</p>
  
  <p>Some kind of a stack, where whilst I add objects, they are asynchronously executed,  as they are added.</p>
  
  <p>If I try something like:</p>
  
  <pre><code>Scheduler::Execute ( asio_service_client &amp; client )
  {
     client.Run( ... )
     io_service.reset();
     io_service.run();
  }
  </code></pre>
  
  <p>I will reset previous running services, and start all over, which is not what I want.
  My only obvious option, is to either accept and assign a separate io_service for each added asio_service_client, or force them to be added all together in a job group, which is then executed?</p>
  
  <p>The other solution I can think of, is using threads, thereby, each asio_service_client will run in its own thread, and thus won't block other asio_service_clients, executing in parallel?</p>