清除boost :: asio :: io_service after stop() [英] Clear boost::asio::io_service after stop()

查看:285
本文介绍了清除boost :: asio :: io_service after stop()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用(单线程)a boost :: asio:io_service 来处理很多tcp连接。对于每个连接,我使用deadline_timer捕捉超时。如果任何连接超时,我可以不使用其他连接的结果。因此我想完全重新启动我的io_service。我认为调用 io_service.stop()会允许队列中的完成处理程序被调用,并且会调用队列中的处理程序,并显示错误。

$但是,看起来处理程序仍然在队列中,因此调用 io_service.reset()和更高版本 io_service .run()将旧的处理程序备份。
任何人都可以确认处理程序确实保留在队列中,即使 io_service.stop()被调用。如果是,什么是完全重置io_service的可能性,例如。删除所有排队的处理程序?

解决方案

io_service :: stop() io_service :: reset() 仅控制 io_service 的事件循环的状态;也不会影响计划用于延迟调用(即时运行)或用户定义的处理程序对象的处理程序的生命周期。



析构函数 io_service 将导致所有未处理的处理程序被销毁:




  • io_service 将调用 shutdown_service()成员函数。根据服务类型要求, shutdown_service()成员函数将销毁服务持有的用户定义的处理程序对象的所有副本。

  • 对于 io_service 及其任何链接,销毁延迟调用。



请考虑:




  • 控制 io_service 对象的生命周期。一种方法可以在答案中找到。

  • 运行 io_service 完成。这通常需要设置状态,取消未完成的操作,并阻止完成处理程序将额外的工作发布到 io_service 中。 Boost.Asio提供了官方 timeout 示例,并且还显示运行到 io_service 以完成的超时方法此处


I am using (single threaded) a boost::asio:io_service to handle a lot of tcp connections. For each connection I use a deadline_timer to catch timeouts. If any of the connections times out, I can use none of the results of the other connections. Therefore I want to completely restart my io_service. I thought that calling io_service.stop() would allow "finished" handlers in the queue to be called and would call handlers in the queue with an error.

However it looks like the handlers remain in the queue and therefore calling io_service.reset() and later io_service.run() brings the old handlers back up. Can anyone confirm that the handlers indeed remain in the queue even after io_service.stop() is called. And if so, what are the possibilities to completly reset the io_service, e.g. remove all queued handlers?

解决方案

io_service::stop() and io_service::reset() only control the state of the io_service's event loop; neither affect the lifespan of handlers scheduled for deferred invocation (ready-to-run) or user-defined handler objects.

The destructor for io_service will cause all outstanding handlers to be destroyed:

  • Each service object associated with the io_service will have its shutdown_service() member function invoked. Per the Service type requirement, the shutdown_service() member function will destroy all copies of user-defined handler objects that are held by the service.
  • Uninvoked handler objects scheduled for deferred invocation are destroyed for the io_service and any of its strands.

Consider either:

  • Controlling the lifespan of the io_service object. One approach can be found in this answer.
  • Running the io_service to completion. This often requires setting state, cancelling outstanding operations, and preventing completion handlers from posting additional work into the io_service. Boost.Asio provides an official timeout example, and a timeout approach with running to the io_service to completion is also shown here.

这篇关于清除boost :: asio :: io_service after stop()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆