Boost C++ 库

...这是世界上备受推崇且设计精良的 C++ 库项目之一。 Herb SutterAndrei Alexandrescu, C++ Coding Standards

计时器 - Boost C++ 函数库
PrevUpHomeNext

长时间运行的 I/O 操作通常会在一个截止时间之前完成。这些截止时间可以表示为绝对时间,但通常是相对于当前时间计算的。

作为简单的例子,使用相对时间执行计时器的同步等待操作可以这样写:

io_context i;
...
steady_timer t(i);
t.expires_after(chrono::seconds(5));
t.wait();

更常见的是,程序将执行计时器的异步等待操作

void handler(boost::system::error_code ec) { ... }
...
io_context i;
...
steady_timer t(i);
t.expires_after(chrono::milliseconds(400));
t.async_wait(handler);
...
i.run();

也可以将与计时器关联的截止时间作为绝对时间获取

steady_timer::time_point time_of_expiry = t.expiry();

这允许组合计时器

steady_timer t2(i);
t2.expires_at(t.expiry() + chrono::seconds(30));
参见

basic_waitable_timer, steady_timer, system_timer, high_resolution_timer, 计时器教程


PrevUpHomeNext