boost::asio 在抛出 io_service::run() 后挂在解析器服务析构函数中

发布于 2024-09-10 21:35:06 字数 328 浏览 3 评论 0原文

我使用相当简单的 boost::asio 设置,我从主线程调用 io_service.run() 。 我有一个 tcp 解析器,并使用异步解析来查找地址。 当查找失败时,我会在异步回调中抛出异常。 我在 main 函数内部的 run() 调用之外捕获了这个异常。然后我在 io_service 实例(这是一个全局实例)上调用 stop() 。 然而,当 main() 返回时,程序挂起。事实证明,它正在等待一个从未来自解析器服务的 exit_event_。

我不想在退出时停留。我做错了什么吗?如果是这样,那又怎样?我在网上没有找到太多关于这些事情的讨论。 我在 Windows 7/64 位上使用 boost 1.41.0。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒江雪… 2024-09-17 21:35:06

然后我在 io_service 上调用 stop()

尝试使用这个技巧(复制自 io_service 文档)当您需要停止 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. 

原因很简单(也来自文档):调用 io_service::stop()< /code> 将导致 io_service run() 调用尽快返回,放弃未完成的操作,并且不允许分派就绪处理程序。

因此,如果您需要分派所有处理程序,则调用 io_service::stop() 是不够的。

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.

暖树树初阳… 2024-09-17 21:35:06

stop() 只是信号io_service 停止。如果您在 stop() 调用之后进行另一个 run() 调用,它应该返回并正确清理。

中有一些关于从处理程序抛出异常的讨论文档

我还猜测这个问题可能与某些对象生命周期问题有关,例如 io 服务被破坏,而其他东西仍在引用它。仔细查看示例以及如何使用共享指针来确保对象仍然存在。

stop() just signals the io_service to stop. If you follow the stop() call with another run() call it should return and clean up properly.

There is some discussion about throwing exceptions from handlers in the documentation.

I'm also guessing the issue may be related to some object lifetime issue, e.g. the io service is destroyed while something else is still referencing it. Take a closer look at the examples and how shared pointers are used to ensure the objects are still around.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文