Home  >  Article  >  Backend Development  >  How to Safely Cancel a boost::asio::basic_waitable_timer?

How to Safely Cancel a boost::asio::basic_waitable_timer?

DDD
DDDOriginal
2024-10-25 11:33:02868browse

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 timers.

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&amp; 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 timers becomes both safe and robust.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn