I bind read_async_some() calls to asynchronously read from the serial port. At some point, I need to cancel asynchronous readings, using and detecting this fact in related handlers. From the documentation for cancel() I expected to do this simply by checking the error_code passed to my handlers:
This function calls all outstanding asynchronous reads or writes of the operation for immediate completion, and boost::asio::error::operation_aborted will be passed to the handlers for canceling boost::asio::error::operation_aborted .
However, when I try to do this, my handlers are called with an invalid_argument error instead of the expected operation_aborted error. Here is a minimal example that reproduces the problem using ptty to emulate a serial port:
void handle(boost::system::error_code const& error, size_t count) { std::cout << "error_code = " << error.message() << std::endl; } int main(int argc, char **argv) { std::fstream fs("/dev/ttyp0", std::ios::in | std::ios::ate); boost::asio::io_service io; boost::asio::serial_port serial(io, "/dev/ttyp0"); std::vector<uint8_t> buffer(1); serial.async_read_some(boost::asio::buffer(buffer), boost::bind(&handle, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred ) ); boost::thread thread(boost::bind(&boost::asio::io_service::run, &io)); serial.cancel(); thread.join(); fs.close(); }
At least for me the output of this program is error_code = Invalid argument . Can someone explain why I do not understand the behavior described in the documentation?
source share