Canceling operations
Some I/O objects, such as sockets or timers, have object-wide cancellation of outstanding asynchronous operations by calling their close()
or cancel()
methods. If an asynchronous operation is canceled, the completion handler will receive an error with the boost::asio::error::operation_aborted
code.
In the following example, a timer is created, and its timeout period is set to five seconds. But after sleeping the main thread for only two seconds, the timer is canceled by calling its cancel()
method, making the completion handler be called with a boost::asio::error::operation_aborted
error code:
#include <boost/asio.hpp> #include <chrono> #include <iostream> #include <thread> using namespace std::chrono_literals; void handle_timeout(const boost::system::error_code& ec) { if (ec == boost::asio::error::operation_aborted) { std::cout << "Timer canceled...