Home >Backend Development >C++ >How to Safely Cancel a boost::asio::basic_waitable_timer?
Cancelling Boost Asio Deadline Timer Safely
This article addresses the proper cancellation of boost::asio::basic_waitable_timer
Problem:
Initially, the proposed solution to cancel a timer via timer.get_io_service().post([&]{timer.cancel();}) might appear to fail. The timer does not abort even after calling cancel().
Reason:
The issue lies in the timer's behavior post-cancellation. It does not terminate outstanding asynchronous operations and simply starts a new asynchronous wait when the completion handler is invoked.
Solution:
To cancel a timer safely, it's essential to check if any asynchronous operations are currently in progress. This check can be performed by comparing the timer's expiry time with the current time:
<code class="cpp">timer.get_io_service().post([](){ if (timer.expires_from_now() >= std::chrono::steady_clock::duration(0)) { timer.cancel(); } });</code>
Enhanced Cancellation:
In addition to canceling the current operation, it can be beneficial to communicate a shutdown signal to the completion handler. This can be achieved by setting the timer's expiry time to a special value like Timer::clock_type::time_point::min().
<code class="cpp">timer.expires_at(Timer::clock_type::time_point::min());</code>
This allows the completion handler to identify the shutdown state:
<code class="cpp">void handle_timeout(const boost::system::error_code& ec) { if (timer.expires_at() != Timer::time_point::min()) { // timer is not in shutdown state // ... } else { // timer is in shutdown state std::cerr << "handle_timeout: detected shutdown\n"; } }</code>
By implementing these techniques, the cancellation of boost::asio::basic_waitable_timer
The above is the detailed content of How to Safely Cancel a boost::asio::basic_waitable_timer?. For more information, please follow other related articles on the PHP Chinese website!