简体   繁体   English

如何检查boost :: deadline_timer是否处于活动状态

[英]how to check if boost::deadline_timer is active

any way to check if deadline_timer is active? 检查deadline_timer是否有效的任何方法? eg if it's in async_wait state and wasn't cancelled? 例如,如果它处于async_wait状态并且未被取消?

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? 那么你不能简单地在调用async_wait之前设置一个bool,然后在设置错误条件的情况下在处理程序中重置它吗?

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. 创建自己的计时器类(当然使用deadline_timer),它有自己的do_async_wait成员。 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. 在此成员中,您可以设置一个成员变量,然后可以在内部deadline_timer上调用async_wait之前从bool isWaiting()成员或类似成员返回该变量。 Note that I don't think that the async_wait member is virtual, so you can't just inherit from deadline_timer and override. 请注意,我不认为async_wait成员是虚拟的,因此您不能仅从deadline_timer继承并覆盖。 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. 请注意,在调用处理程序时(通过使自己的计时器成为处理程序然后转发到任何其他处理程序)或者调用cancel时,您还需要重置标志。

  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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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