我正在尝试运行以下代码:
some_sock.async_connect(...); // handle_connect() sets the 'condition' flag
boost::asio::deadline_timer t(ios, boost::posix_time::seconds(2));
while (t.expires_from_now() >= boost::posix_time::seconds(0))
{
ios.run_one();
if (condition) return;
}
在计时器t到期后(2秒后),从run_one()返回所需的行为。 实际上,run_one()会阻塞,直到收到SYN-ACK或RST。如果服务器没有响应,run_one()将阻止超过2秒的超时。
我应该怎么做才能等待指定的时间连接在后台进行一些工作?
感谢。
答案 0 :(得分:4)
使用async tcp client example中描述的io_service::run
和deadline_timer::async_wait
。
答案 1 :(得分:0)
在另一个线程中为connect()调用run_one()。在超时时调用some_sock.cancel()。 (首先阅读其文档)。