Boost C++ 库

...世界上最受尊敬和精心设计的 C++ 库项目之一。 Herb SutterAndrei Alexandrescu, C++ 编码规范

定时器 - 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