提振::在解析服务的析构函数ASIO挂起io_service对象的抛出::运行后() [英] boost::asio hangs in resolver service destructor after throwing out of io_service::run()
问题描述
我使用的是一个相当简单的boost :: ASIO设置,在这里我呼吁io_service.run()主线程。
我有一个TCP解析器,并使用异步解决查找的地址。
当查找失败,我扔异步回调内部异常。
我赶上这种异常的run()调用外,主要功能里面。然后,我呼吁我的io_service对象实例的stop()(这是一个全球性的)。
然而,当主()返回时,程序挂起。原来,在等待永远不会来自解析服务的exit_event_。
我不希望挂在出境。是不是我做错了吗?如果有,是什么?我还没有发现这些事情很多讨论网上。
我使用升压1.41.0在Windows 7/64位。
我然后调用停止()在我的io_service对象
块引用>尝试使用这一招(从复制io_service对象文档)当您需要停止
io_service对象
:的boost ::支持ASIO :: io_service对象io_service对象;
auto_ptr的<提高:: ASIO :: io_service对象::工作>工作(
新的boost ::支持ASIO :: io_service对象::工作(io_service对象));
...
work.reset(); //允许的run()退出。原因很简单(也是从文档):调用
io_service对象::停止()
将导致io_service对象的run()调用尽可能很快返回,放弃未完成的操作,并没有允许随时处理程序进行调度。所以,调用
io_service对象::停止()
是不够的,如果你需要派遣所有的处理程序。I'm using a fairly simple boost::asio set-up, where I call io_service.run() from the main thread. I have a tcp resolver, and use async resolve to look up an address. When that look-up fails, I throw an exception inside the asynchronous callback. I catch this exception outside the run() call, inside the main function. I then call stop() on my io_service instance (which is a global). However, when main() returns, the program hangs. It turns out to be waiting for an exit_event_ that never comes from the resolver service.
I do not wish to hang on exit. Is there something I'm doing wrong? If so, what? I haven't found much discussion about these things online. I'm using boost 1.41.0 on Windows 7/64bit.
解决方案I then call stop() on my io_service
Try to use this trick (copied from io_service documentation) when you need to stop
io_service
:boost::asio::io_service io_service; auto_ptr<boost::asio::io_service::work> work( new boost::asio::io_service::work(io_service)); ... work.reset(); // Allow run() to exit.
The reason is simple (also from the documentation): call to
io_service::stop()
will cause the io_service run() call to return as soon as possible, abandoning unfinished operations and without permitting ready handlers to be dispatched.So, calling
io_service::stop()
is not enough if you need to dispatch all handlers.这篇关于提振::在解析服务的析构函数ASIO挂起io_service对象的抛出::运行后()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!