VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
timer.h File Reference

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

#include <cstdint>
#include <functional>
#include <memory>
#include "./macros.h"
Include dependency graph for timer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

 Event-loop-driven repeating or one-shot timer. More...

Namespaces

Detailed Description

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().
Note
  • 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();