title: Event queue using boost::asio and strands: Waiting for new events
body: <p>So I want to make a synchronous event queue in c++ on a custom thread. As far as I can tell, boost::asio::strand is an excelent candidate for this, with one twist: when asio::run() is called, it only runs while there are events in the strand's queue. The code:</p>
  
  <pre><code>this-&gt;control_strand_.reset(new boost::asio::strand(control_io_service_));
  control_thread_ = boost::thread(boost::bind(&amp;boost::asio::io_service::run,&amp;control_io_service_));
  control_thread_.join();
  </code></pre>
  
  <p>Returns immediately. Now I could go with the answer of <a href="https://stackoverflow.com/questions/13517313/boost-asio-how-to-know-when-the-handler-queue-is-empty">Boost Asio - How to know when the handler queue is empty?</a>, but this has a while-loop-wait in it. I'd rather have it be more event based (aka, wait for a "wrap" call in the while look when the queue is empty). Th only way I can think to do this is completely wrap the strand class, having it trigger a signal whenever "wrap" is called (something like, pseudo code)</p>
  
  <pre><code>//some member variables
  boost::condition_variable cond_var;
  boost::mutex mut;
  std::unique_ptr&lt;boost::asio::strand&gt; control_strand_;
  boost::asio::io_service control_io_service_
  //while loop,running on event processing thread
  void MessageProcessor()
  {
    while (true)
    {
      {
        boost::unique_lock&lt;boost::mutex&gt; lock(mut);
        cond_var.wait(lock);
      }
      control_io_service_.run();
    }
  }
  //post call,from different thread
  template &lt;typename Handler&gt;
  void wrap(Handler hand)
  {
    cond_var.notify_all();
    control_strand_-&gt;wrap(hand);
  }
  </code></pre>
  
  <p>This will run the queue forever without the while loop (my synchronization is a little off, but thats not an issue atm). Is there a better, more standard, way?</p>