cancel a deadline_timer, callback triggered anyway

后端 未结 2 985
醉话见心
醉话见心 2021-01-05 22:09

I was suprised not to find a clock component in boost::asio (our any widely used library) so it tried making a simple, minimalistic, implementation for testing some of my co

2条回答
  •  清酒与你
    2021-01-05 22:13

    From the boost documentation:

    If the timer has already expired when cancel() is called, then the handlers for asynchronous wait operations will:

    1. have already been invoked;
    2. or have been queued for invocation in the near future.

    These handlers can no longer be cancelled, and therefore are passed an error code that indicates the successful completion of the wait operation.

    Your application on such successful completions (when timer already expired) restart the timer again, and another interesting thing that on Start function call you again implicitly cancelling the timer in case when it is not expired yet.

    expires_at function sets the expiry time. Any pending asynchronous wait operations will be cancelled. The handler for each cancelled operation will be invoked with the boost::asio::error::operation_aborted error code.

    Probably you could reuse your m_enabled variable or just have another flag for detecting timer cancellation.

    Another solution is possible: timer example

提交回复
热议问题