[关闭]
@john-lee 2021-01-03T11:56:23.000000Z 字数 810 阅读 553

定时器(Timers)

Boost.Asio


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

作为一个简单的示例,使用相对时间对计时器执行同步等待操作,可以编写:

  1. io_context i;
  2. ...
  3. deadline_timer t(i);
  4. t.expires_from_now(boost::posix_time::seconds(5));
  5. t.wait();

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

  1. void handler(boost::system::error_code ec) { ... }
  2. ...
  3. io_context i;
  4. ...
  5. deadline_timer t(i);
  6. t.expires_from_now(boost::posix_time::milliseconds(400));
  7. t.async_wait(handler);
  8. ...
  9. 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)

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注