【发布时间】:2016-02-26 12:35:06
【问题描述】:
我尝试等待从任何boost::asio::async_ 函数返回的std::future 对象(使用use_future)。例如:
auto endpoint_future = resolver.async_resolve(query, boost::asio::use_future);
endpoint_future.wait(); // lasts forever after io_service.stop();
这是在一个线程中完成的。
我还有另一个线程在此async_resolve 调用之前启动:
runner_ = std::thread([this]() {
boost::asio::io_service::work work(io_service_);
io_service_.run();
});
一切正常,但后来我还添加了一个boost::asio::deadline_timer 以停止与io_service 的任何工作:
void deadlineTimeout() {
deadline_.cancel();
io_service_.stop();
// closing socket here does not work too
}
但是,在deadlineTimeout() 中,当截止日期到达它的超时并且它执行io_service_.stop() 未来没有被释放所以endpointer_future.wait() 仍然阻塞。在这种情况下,我该如何停止等待未来?
【问题讨论】:
标签: c++ multithreading c++11 boost boost-asio