title: avoiding busy wait with boost::asio poll
body: <p>I'm writing a service on linux that uses boost::asio::io_service with io_service::poll in a while loop.  This is a busy wait loop, e.g. it wastes CPU cycles.</p>
  
  <pre><code>void Application::run()
  {
      try
      {
          std::cout &lt;&lt; "TcpServer starting..\n";
          _TcpServer.reset( new TcpServer(_io_service, boost::ref(_spState)) );
  
          // _io_service.run();
  
          while( ! _spState-&gt;QuitSignalled() )
          {
              _io_service.poll(); 
          }
  
          std::cerr &lt;&lt; "quit signalled, TcpServer stopping.. :/" &lt;&lt; std::endl;
      }
      catch(std::exception &amp; e)
      {
          std::cout &lt;&lt; e.what() &lt;&lt; "\n";
      }
  }
  </code></pre>
  
  <p>I use poll instead of run to check if another thread in the service has signalled service shutdown.</p>
  
  <p>Is there a way of achieving this without using poll in a busy wait loop ?</p>
  
  <p>The service uses async io on a single thread, and other threads do data processing.</p>
  
  <p>I've added a sleep between iterations of the loop which seems to reduce the waste of cpu time, but I was hoping there might be a more efficient way?</p>
  
  <pre><code>void Application::run()
  {
      using boost::this_thread::sleep_for;
  
      static const boost::chrono::milliseconds napMsecs( 50 );
  
      try
      {
          std::cout &lt;&lt; "TcpServer starting..\n";
          _TcpServer.reset( new TcpServer(_io_service, boost::ref(_spState)) );
  
          // _io_service.run();
  
          while( ! _spState-&gt;QuitSignalled() )
          {
              _io_service.poll();
              boost::this_thread::sleep_for( napMsecs );
          }
  
          std::cerr &lt;&lt; "quit signalled, TcpServer stopping.. :/" &lt;&lt; std::endl;
      }
      catch(std::exception &amp; e)
      {
          std::cout &lt;&lt; e.what() &lt;&lt; "\n";
      }
  }
  </code></pre>