title: boost asio udp socket async_receive_from does not call the handler
body: <p>I want to create an autonomous thread devoted only to receive data from an UDP socket using boost libraries (asio). This thread should be an infinite loop triggered by some data received from the UDP socket. In my application I need to use an asynchronous receive operation.</p>
  
  <p>If I use the synchronous function receive_from everything works as expected.</p>
  
  <p>However if I use async_receive_from the handler is never called. Since I use a semaphore to detect that some data have been received, the program locks and the loop is never triggered. </p>
  
  <p>I have verified (with a network analyzer) that the sender device properly sends the data on the UDP socket.</p>
  
  <p>I have isolated the problem in the following code.</p>
  
  <pre><code>#include &lt;boost\array.hpp&gt;
  #include &lt;boost\asio.hpp&gt;
  #include &lt;boost\thread.hpp&gt;
  #include &lt;boost\interprocess\sync\interprocess_semaphore.hpp&gt;
  
  #include &lt;iostream&gt;
  
  typedef boost::interprocess::interprocess_semaphore Semaphore;
  
  using namespace boost::asio::ip;
  
  class ReceiveUDP
  {
  public:
  
      boost::thread*  m_pThread;
  
      boost::asio::io_service         m_io_service;
      udp::endpoint                   m_local_endpoint;
      udp::endpoint                   m_sender_endpoint;
  
      udp::socket                     m_socket;
  
      size_t      m_read_bytes;
      Semaphore   m_receive_semaphore;
  
      ReceiveUDP() :
          m_socket(m_io_service),
          m_local_endpoint(boost::asio::ip::address::from_string("192.168.0.254"), 11),
          m_sender_endpoint(boost::asio::ip::address::from_string("192.168.0.11"), 5550),
          m_receive_semaphore(0)
      {
          Start();
      }
  
      void Start()
      {
          m_pThread = new boost::thread(&amp;ReceiveUDP::_ThreadFunction, this);
      }
  
      void _HandleReceiveFrom(
          const boost::system::error_code&amp; error,
          size_t                                  received_bytes)
      {
          m_receive_semaphore.post();
  
          m_read_bytes = received_bytes;
      }
  
      void _ThreadFunction()
      {
          try
          {
              boost::array&lt;char, 100&gt; recv_buf;
  
              m_socket.open(udp::v4());
              m_socket.bind(m_local_endpoint);
              m_io_service.run();
  
              while (1)
              {
  #if 1 // THIS WORKS
  
                  m_read_bytes = m_socket.receive_from(
                      boost::asio::buffer(recv_buf), m_sender_endpoint);
  
  #else // THIS DOESN'T WORK
  
                  m_socket.async_receive_from(
                      boost::asio::buffer(recv_buf),
                      m_sender_endpoint,
                      boost::bind(&amp;ReceiveUDP::_HandleReceiveFrom, this,
                      boost::asio::placeholders::error,
                      boost::asio::placeholders::bytes_transferred));
  
                  /* The program locks on this wait since _HandleReceiveFrom
                  is never called. */
                  m_receive_semaphore.wait();
  
  #endif
  
                  std::cout.write(recv_buf.data(), m_read_bytes);
              }
  
              m_socket.close();
          }
          catch (std::exception&amp; e)
          {
              std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
          }
      }
  };
  
  void main()
  {
      ReceiveUDP  receive_thread;
  
      receive_thread.m_pThread-&gt;join();
  }
  </code></pre>
  
  <p>A timed_wait on the semaphore is to be preferred, however for debug purposes I have used a blocking wait as in the code above.</p>
  
  <p>Did I miss something? Where is my mistake?</p>