清除的boost ::支持ASIO ::停止后io_service对象() [英] Clear boost::asio::io_service after stop()
问题描述
我使用(单线程)一个的boost :: ASIO:io_service对象
来处理大量的TCP连接。对于每个连接我用deadline_timer赶上超时。如果任何连接超时的,我可以用没有其他连接的结果。因此,我要完全重新启动我的io_service对象。我认为叫 io_service.stop()
将允许完成的队列中的处理程序被调用,将要求在队列处理程序有错误。
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.
然而,它看起来像处理程序保留在队列中,因此调用 io_service.reset()
后来 io_service.run()
带来的旧处理备份。
任何人都可以证实,确实处理留在队列中即使 io_service.stop()
被调用。如果是的话,什么都完全地重置io_service对象,例如准备删除所有排队的处理程序?
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对象::停止()
和的 io_service对象::复位()
只控制状态在 io_service对象
的事件循环;既不影响计划延期调用句柄(立即运行)或用户定义的处理程序对象的生命周期。
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.
的<一个href=\"http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/io_service/_io_service.html\"相对=nofollow>为 io_service对象
>将导致销毁了所有优秀的处理程序:
The destructor for io_service
will cause all outstanding handlers to be destroyed:
- 与
io_service对象
将其shutdown_service相关联的每个服务对象()
成员函数调用。按照服务类型的要求,shutdown_service()
成员函数将会破坏由服务所拥有的用户定义的处理程序对象的所有副本。 - 计划延期调用未调用处理程序对象被销毁的
io_service对象
及其任何股。
- Each service object associated with the
io_service
will have itsshutdown_service()
member function invoked. Per the Service type requirement, theshutdown_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.
考虑之一:
- 控制
io_service对象
对象的生命周期。一种方法可以在找到这个答案 - 运行
io_service对象
完成。这往往需要设置状态,取消未完成的操作,和preventing完成处理程序可以发布更多的工作纳入io_service对象
。 Boost.Asio的提供了一个正式的<一个href=\"http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/examples/cpp03_examples.html#boost_asio.examples.cpp03_examples.timeouts\"相对=nofollow>超时例子,与运行在io_service对象
来完成一个超时的做法也显示的这里。
- 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 theio_service
. Boost.Asio provides an official timeout example, and a timeout approach with running to theio_service
to completion is also shown here.
这篇关于清除的boost ::支持ASIO ::停止后io_service对象()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!