@john-lee
2021-01-03T11:56:23.000000Z
字数 810
阅读 553
Boost.Asio
长时间运行的 I/O 操作通常有一个必须完成的截止日期。这些截止日期可以表示为绝对时间,但通常是相对于当前时间计算的。
作为一个简单的示例,使用相对时间对计时器执行同步等待操作,可以编写:
io_context i;
...
deadline_timer t(i);
t.expires_from_now(boost::posix_time::seconds(5));
t.wait();
更常见的情况是,程序将在计时器上执行异步等待操作:
void handler(boost::system::error_code ec) { ... }
...
io_context i;
...
deadline_timer t(i);
t.expires_from_now(boost::posix_time::milliseconds(400));
t.async_wait(handler);
...
i.run();
与计时器关联的截止时间也可以作为相对时间获得:
boost::posix_time::time_duration time_until_expiry
= t.expires_from_now();
或作为允许计时器组成的绝对时间:
deadline_timer t2(i);
t2.expires_at(t.expires_at() + boost::posix_time::seconds(30));
basic_deadline_timer, deadline_timer,计时器教程。
Copyright © 2003-2020 Christopher M. Kohlhoff
Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)