【问题标题】:Boost.Asio: The I/O operation has been aborted because of either a thread exit or an application requestBoost.Asio:由于线程退出或应用程序请求,I/O 操作已中止
【发布时间】:2017-11-12 21:19:52
【问题描述】:

我正在学习 Boost.Asio。我创建了一个简单的程序来将主机名解析为 IP 地址。使用同步解析操作时它工作正常。但是,当我尝试异步方式时,出现了一些奇怪的行为。

#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>

using boost::asio::ip::tcp;

void resolver_handler(
    const boost::system::error_code& err,
    tcp::resolver::iterator it
) {
    if (err) {
        std::cerr << "Resolver error: " << err.message() << std::endl;
        return;
    }
    tcp::resolver::iterator end;
    while (it != end) {
        std::cout << "Host name: " << it->host_name() << std::endl;
        std::cout << "Endpoint: " << it->endpoint() << std::endl;
        std::cout << "Service name: " << it->service_name() << std::endl;
        ++it;
    }
}

void resolve_host(boost::asio::io_service& io_service) {
    tcp::resolver::query query("www.google.com", "http");
    tcp::resolver resolver(io_service);
    resolver.async_resolve(
        query,
        boost::bind(
            resolver_handler,
            boost::asio::placeholders::error,
            boost::asio::placeholders::iterator
        )
    );
    std::cout << "Bind" << std::endl;  // <<<----This line
}

int main(int argc, char **argv) {
    try {
        boost::asio::io_service io_service;
        resolve_host(io_service);
        io_service.run();
    } catch (std::exception& e) {
        std::cerr << "Exception: " << e.what() << std::endl;
    }
    return 0;
}

resolve_host函数的最后一行被注释掉时,报告

Resolver error: The I/O operation has been aborted because of either a thread exit or an application request

当该行存在时,它会给出正确的输出

Bind
Host name: www.google.com
Endpoint: 216.58.219.4:80
Service name: http

我所做的是打印出一些东西。我尝试在async_resolve 调用之后添加一些更简单的逻辑(例如int a = 1;),但它不起作用。在我看来,这是一个时间问题。也许有些东西退出得太快了。

我搜索此错误消息,但发现大多数帖子都是关于 C# 的。我相信这个错误消息不是来自 Boost 而是来自 Windows 系统。

谁能向我解释为什么会发生这种情况?非常感谢。

【问题讨论】:

  • resolve_host 返回时,tcp::resolver 不再存在,因为您在被销毁的堆栈上创建了它。它应该如何异步执行查询?某些东西必须拥有该解析器。有很多选择,但总得做点什么。
  • @DavidSchwartz 啊哈,没错。谢谢。

标签: c++ boost boost-asio asio


【解决方案1】:

就像@David Schwartz 所说,您必须让解析器保持活动状态才能完成异步操作。

这是我能想到的最简单的方法:

void resolve_host(boost::asio::io_service& io_service) {
    tcp::resolver::query query("www.google.com", "http");
    auto resolver = std::make_shared<tcp::resolver>(io_service);
    resolver->async_resolve(
        query,
        [resolver](auto ec, auto it) { resolver_handler(ec, it); }
    );
}

请注意,我使用 lambda 来捕获解析器(它是动态分配的,而不是在堆栈上)。

如果您没有该选项,最简单的方法是将一些不透明的上下文绑定到完成处理程序bind

using opaque_context = std::shared_ptr<void const>;

void resolver_handler(
    const boost::system::error_code& err,
    tcp::resolver::iterator it,
    opaque_context
) {
    // ...

然后使用

resolver->async_resolve(
    query,
    boost::bind(resolver_handler, ph::error, ph::iterator, resolver)
);

查看在 Coliru 上编译c++03 onlyC++11 opaque_contextc++11 lambda

请注意,我可能会传递 endpoint 而不是 iterator 以进一步解耦代码(参见例如 ASIO getting a tcp endpoint directly from an asynchronous resolve)。但您不需要这样做,因为 iterator 确实 保持关联状态处于活动状态:What's the lifetime of boost::asio::ip::tcp::resolver::iterator from async_resolve?

【讨论】:

  • 很棒的答案。谢谢!
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 2014-02-17
  • 2014-07-03
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2021-01-02
相关资源
最近更新 更多