#include #include void writeComplete(const boost::system::error_code & ec, size_t transferred) { if (ec) { // cancel was called before the io_service was entered. I would // expect an operation aborted error std::cout << "operation cancelled" << std::endl; } else { std::cout << transferred << " bytes transferred (not cancelled!)" << std::endl; } } int main(int argc, char * argv[]) { boost::asio::io_service my_io_service; char msg[] = {'h', 'e', 'l', 'l', '0', 0}; { boost::asio::serial_port port(my_io_service, "/dev/ttyS1"); // serial port is opened and we initiate an async write to probe for a device // attached boost::asio::async_write(port, boost::asio::const_buffers_1(msg, sizeof(msg)), writeComplete); // assume elsewhere in the code an error occurred, and we cancel all processing port.cancel(); // the cleanup of the error also causes the serial port to be destroyed. } // meanwhile the error cleanup has completed and we're ready to continue without using // the serial port std::cout << "entering io_service" << std::endl; my_io_service.run(); }