简体   繁体   中英

how to check if boost::deadline_timer is active

any way to check if deadline_timer is active? eg if it's in async_wait state and wasn't cancelled?

The handler for the timeout will be called with an error condition if the timer was canceled (as well as a normal timeout). So could you not simply set a bool before calling async_wait and then reset that in the handler if the error condition is set?

Three options come to mind:

  1. Create your own timer class (by using the deadline_timer of course) that has its own do_async_wait member. In this member you set a member variable which can then be returned from a bool isWaiting() member or similar, before you call the async_wait on the internal deadline_timer. Note that I don't think that the async_wait member is virtual, so you can't just inherit from deadline_timer and override. Note that you would also need to reset the flag when your handler is called (by making your own timer the handler that then forwards to any other handler) or when cancel is called.

  2. Edit the boost code to do what you want. This is perfectly legal as far as I am aware, however this is of course not a very good option.

  3. Alter your requirements/design so that you don't need to know this.

How about this?

boost::asio::deadline_timer mTimer;
const bool timerExpired = (mTimer.expires_at()
  <= boost::posix_time::second_clock::local_time());

I know that this is a bit old at this point, but I have another possible suggestion: how about getting the implementation and then requesting if there are possibly pending waits. It would look like this:

mTimer.get_implementation().might_have_pending_waits

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM