【发布时间】:2016-02-02 11:09:49
【问题描述】:
我编写了复杂的 TCP/IP 客户端应用程序,它会生成大量的传出 TCP 连接。我尝试使用boost::asio 作为 C++ 中的可移植 TCP/IP 网络实现。
我发现了以下问题。见代码:
int main(int argc, char *argv[])
{
using namespace boost::asio;
io_service io;
thread ioThread([&io]()
{
while (true)
{
if (!io.run())
this_thread::sleep_for(seconds(1));
}
});
ioThread.detach();
ip::tcp::resolver r(io);
ip::tcp::resolver::query q("www.boost.org", "80");
atomic<bool> done(false);
r.async_resolve(q, [&done](const boost::system::error_code& error, ip::tcp::resolver::iterator iterator)
{
if (error)
wprintf(W("Error!\r\n"));
else
{
ip::tcp::resolver::iterator end;
while (iterator != end)
{
ip::tcp::endpoint endpoint = *iterator++;
std::cout << endpoint << std::endl;
}
}
done.store(true);
});
while (!done.load())
this_thread::sleep_for(seconds(1));
return 0;
}
此代码运行后台线程来执行io_service::run 方法。假设我们将有多个套接字使用这个io_service 来执行异步操作。
在 Windows (Visual Studio 2013 x64) 上,此代码运行良好并打印端点地址。
在带有 boost 1.60 和 clang 3.4.2 的 CentOS Linux 6 上它挂起。
有什么想法为什么async_resolve 永远不会完成吗?
【问题讨论】:
-
您包含的代码无法编译,minimal reproducible example.
-
代码编译没有问题,当然,如果你添加了所需的头文件。在 VS 2013 和 clang 3.4.2 上测试。
标签: c++ linux sockets boost tcp