title: Flushing all asynchronous handlers in boost::asio
body: <p>I'm running some tests that need asynchronous communication, and the underlying framework is Asio.  Sometimes, a handler is kept in the processing loop even if the test has been tore down, for good reasons.  But then, it is called <em>after</em> the targets are deleted.</p>
  
  <p>The <code>Test</code> class:</p>
  
  <pre><code>virtual void SetUp()
  {
    _client = new Client;
    _server = new Server;
  
    Service::run();
  }
  
  virtual void TearDown()
  {
    Service::stop();
  
    delete _client;
    delete _server;
  }
  </code></pre>
  
  <p>The <code>Service</code> class:</p>
  
  <pre><code>static void run()
  {
    _thread = new asio::thread(boost::bind(&amp;asio::io_service::run, _service));
  }
  
  static void stop()
  {
    _service-&gt;stop();
  
    _thread-&gt;join();
    delete _thread;
  
    _service-&gt;reset();
  }
  </code></pre>
  
  <p><code>io_service::stop()</code> is non-blocking so it gets quite useless in my case. If I delete the <code>io_service</code> object at the end of the function, the handler won't be called, but I'd like a better solution to force completion <em>before</em> the objects are deleted.</p>
  
  <p>Note: the actual processing loop is done in a second thread, but it is joined in a <code>io_service::stop()</code> wrapper, and the whole problem doesn't seem to be thread-related.</p>
  
  <p>I'm using Asio (non-Boost) 1.4.5, but could consider upgrading (to get the <code>io_service::stopped()</code> operation?).</p>
  
  <p>EDIT: Add the <code>io_service</code> wrapper code as it seems to be relevant according to the comments.</p>