VLink 2.0.0
A high-performance communication middleware
载入中...
搜索中...
未找到
timer.h 文件参考

Event-loop-driven periodic/one-shot timer with configurable priority. 更多...

#include <cstdint>
#include <functional>
#include <memory>
#include "./macros.h"
timer.h 的引用(Include)关系图:
此图展示该文件被哪些文件直接或间接地引用了:

浏览该文件的源代码.

 Event-loop-driven repeating or one-shot timer. 更多...

命名空间

详细描述

Event-loop-driven periodic/one-shot timer with configurable priority.

Timer integrates with a MessageLoop to deliver callbacks on its thread. Unlike platform timers, the callback runs serially with all other tasks posted to the same loop, so no extra synchronisation is needed inside the callback.

Key features:

  • Repeating or one-shot mode (kInfinite for loop_count means repeat forever).
  • Optional strict mode: if the loop is busy and the next tick is missed, strict mode fires the missed callbacks immediately to maintain the schedule.
  • Priority support for kPriorityType message loops.
  • Minimum timer resolution is kMinInterval (10000) to prevent busy-wait overhead.
  • A detached Timer does not fire until attached to a loop via attach().

Lifecycle:

  1. Construct a Timer (with or without a MessageLoop).
  2. Optionally call attach() to bind to a loop.
  3. Call start() to arm the timer.
  4. Call stop() to disarm; restart() to reset the countdown.
  5. Destruction automatically calls stop() and detach().
注解
  • When interval_ms is zero, the interval falls back to kMinInterval (10000).
  • A timer is automatically stopped when its MessageLoop is destroyed.
  • call_once() is a convenience factory for fire-and-forget one-shot timers.
Example
vlink::Timer timer(&loop, 500, vlink::Timer::kInfinite, []() {
// called every 500 ms on the loop thread
});
timer.start();
loop.run();