ADBlock is blocking some content on the site

ADBlock errore

How do I perform a nonblocking read using asio?

StackOverflow https://stackoverflow.com/questions/456042


I am attempting to use boost::asio to read and write from a device on a serial port. Both boost::asio:read() and boost::asio::serial_port::read_some() block when there is nothing to read. Instead I would like to detect this condition and write a command to the port to kick-start the device.

How can I either detect that no data is available?

If necessary I can do everything asynchronously, I would just rather avoid the extra complexity if I can.


You have a couple of options, actually. You can either use the serial port's built-in async_read_some function, or you can use the stand-alone function boost::asio::async_read (or async_read_some).

You'll still run into the situation where you are effectively "blocked", since neither of these will call the callback unless (1) data has been read or (2) an error occurs. To get around this, you'll want to use a deadline_timer object to set a timeout. If the timeout fires first, no data was available. Otherwise, you will have read data.

The added complexity isn't really all that bad. You'll end up with two callbacks with similar behavior. If either the "read" or the "timeout" callback fires with an error, you know it's the race loser. If either one fires without an error, then you know it's the race winner (and you should cancel the other call). In the place where you would have had your blocking call to read_some, you will now have a call to io_svc.run(). Your function will still block as before when it calls run, but this time you control the duration.

Here's an example:

void foo()
  io_service     io_svc;
  serial_port    ser_port(io_svc, "your string here");
  deadline_timer timeout(io_svc);
  unsigned char  my_buffer[1];
  bool           data_available = false;

      boost::bind(&read_callback, boost::ref(data_available), boost::ref(timeout),
  timeout.async_wait(boost::bind(&wait_callback, boost::ref(ser_port),

  io_svc.run();  // will block until async callbacks are finished

  if (!data_available)

void read_callback(bool& data_available, deadline_timer& timeout, const boost::system::error_code& error, std::size_t bytes_transferred)
  if (error || !bytes_transferred)
    // No data was read!
    data_available = false;

  timeout.cancel();  // will cause wait_callback to fire with an error
  data_available = true;

void wait_callback(serial_port& ser_port, const boost::system::error_code& error)
  if (error)
    // Data was read and this timeout was canceled

  ser_port.cancel();  // will cause read_callback to fire with an error

That should get you started with only a few tweaks here and there to suit your specific needs. I hope this helps!

Another note: No extra threads were necessary to handle callbacks. Everything is handled within the call to run(). Not sure if you were already aware of this...


Its actually a lot simpler than the answers here have implied, and you can do it synchronously:

Suppose your blocking read was something like this:

size_t len = socket.receive_from(boost::asio::buffer(recv_buf), sender_endpoint);

Then you replace it with

size_t len = 0;
error = boost::asio::error::would_block;
while (error == boost::asio::error::would_block)
    //do other things here like go and make coffee
    len = socket.receive_from(boost::asio::buffer(recv_buf), sender_endpoint, 0, error);
std::cout.write(recv_buf.data(), len);

You use the alternative overloaded form of receive_from which almost all the send/receive methods have. They unfortunately take a flags argument but 0 seems to work fine.

You have to use the free-function asio::async_read.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow