Time

class Timer

Allows setting a duration and checking if it has timed out.

Public Functions

explicit Timer(int64_t duration_ms)

Constructs a Timer with the specified duration.

Parameters:

duration_ms – Duration in milliseconds.

void reset()

Resets the timer, starting from the current time.

bool hasTimedOut() const

Checks if the timer has timed out.

Returns:

True if the timer has timed out, false otherwise.

Private Members

int64_t duration_ms_

The duration in milliseconds.

std::chrono::time_point<std::chrono::steady_clock> start_time_

The start time of the timer.

class Chronometer

Allows measuring durations using system time.

Public Functions

void start()

Starts or restarts the chronometer.

int64_t stop()

Stops the chronometer and returns the elapsed time.

Returns:

Elapsed time in milliseconds.

int64_t elapsed() const

Returns the elapsed time without stopping the chronometer.

Returns:

Elapsed time in milliseconds.

Private Members

bool running_ = false

Indicates whether the chronometer is running.

std::chrono::time_point<std::chrono::steady_clock> start_time_

Start time.

std::chrono::time_point<std::chrono::steady_clock> end_time_

End time.

class Scheduler

Allows scheduling tasks to run after a delay or at regular intervals using PersistentWorker.

Public Functions

Scheduler()

Constructs a Scheduler.

void schedule_once(int64_t delay_ms, const std::function<void()> &task)

Schedules a task to run once after a delay.

Parameters:
  • delay_ms – Delay in milliseconds before the task is executed.

  • task – The task to execute.

void schedule_repeating(const std::string &name, int64_t interval_ms, const std::function<void()> &task)

Schedules a task to run repeatedly at fixed intervals.

Parameters:
  • name – A unique name for the task.

  • interval_ms – The interval in milliseconds between executions.

  • task – The task to execute.

void cancel(const std::string &name)

Cancels a scheduled repeating task.

Parameters:

name – The unique name of the task to cancel.

~Scheduler()

Destructor ensures all repeating tasks are cleaned up.

Private Members

std::unique_ptr<PersistentWorker> persistent_worker_

Worker for managing repeating tasks.

std::vector<std::unique_ptr<Thread>> one_time_tasks_

Threads for one-time tasks.