title: asio service handler for stdin keypress
body: <p>I have adapted <a href="http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/tutorial/tuttimer3.html" rel="nofollow noreferrer">step 3 of the Boost asio tutorial</a> to run forever, and display "tick" and "tock" once per second instead of the counter:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;boost/asio.hpp&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;boost/date_time/posix_time/posix_time.hpp&gt;
  
  void print(const boost::system::error_code&amp; /*e*/,
      boost::asio::deadline_timer* t, int* count)
  {
      if( !((*count) % 2) )
          std::cout &lt;&lt; "tick\n";
      else
          std::cout &lt;&lt; "tock\n";
  
      ++(*count);
  
      t-&gt;expires_at(t-&gt;expires_at() + boost::posix_time::seconds(1));
      t-&gt;async_wait(boost::bind(print,
            boost::asio::placeholders::error, t, count));
  }
  
  int main()
  {
    boost::asio::io_service io;
  
    int count = 0;
    boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
    t.async_wait(boost::bind(print,
          boost::asio::placeholders::error, &amp;t, &amp;count));
  
    io.run();
  
    std::cout &lt;&lt; "Final count is " &lt;&lt; count &lt;&lt; "\n";
  
    return 0;
  }
  </code></pre>
  
  <p>Now I want to asynchronously be able to handle a keypress on stdin.  Is there an io_service handler I can use to respond to keypresses, without blocking sleeps or waits?</p>
  
  <p>For example, I'd like to be able to implement a handler function similar to:</p>
  
  <pre><code>void handle_keypress(const boost::error_code&amp;,
      char c)
  {
      std::cout &lt;&lt; "Tap '" &lt;&lt; c &lt;&lt; "'\n";
  }
  </code></pre>
  
  <p>And I would expect my invocation of this handler to be something along the lines of:</p>
  
  <pre><code>  char c = 0;
    boost::asio::stdin_receiver sr(io);
    st.async_wait(boost::bind(handle_keypress, boost::asio::placeholders::error, &amp;c));
  
    io.run();
  </code></pre>
  
  <p>Is this something I can do with asio, either by using a builtin service handler, or writing my own?</p>
  
  <h2>EDIT, ELABORATION:</h2>
  
  <p>I have seen <a href="https://stackoverflow.com/questions/2831253/using-boostasioasync-read-with-stdin">this question</a>, but the linked-to code in the accpeted answer simply does this in <code>main</code>:</p>
  
  <pre><code> while (std::cin.getline(
  </code></pre>
  
  <p>The application I'm writing isn't this simple tick-tock-tap gizmo I've outlined above, but will be a multicast server.  Several worker threads will be sending packets to multicast groups, responding to messages from the main thread, and sending messages back to the main thread.  The application, in turn, will be "driven" by input from the stdin -- for example, when the user presses the "P" key, multicast broadcast will be paused, and when the hit "Q" the whole thing will shut down.  In the main thread, all I'll do in response to these inputs is send messages to the worker threads.</p>
  
  <p>The <code>while</code> loop above won't work in my scenario because while it's waiting for stdin input from the user, the main thread will not be able to process messages coming in from the worker threads.  Some of those messages from the worker thread will generate output to stdout.</p>