cxxitimer 2.0.5
A C++ library to handle linux interval timer
Loading...
Searching...
No Matches
cxxitimer

Introduction

cxxitimer is a C++ library that provides a convenient wrapper around the Linux interval timer API (setitimer / getitimer). It exposes three timer types that map directly to the POSIX interval timers, each enforcing a single-instance-per-process constraint at runtime.

Timer Types

Class POSIX timer Signal Measures
cxxitimer::ITimer_Real ITIMER_REAL SIGALRM Wall-clock (real) time
cxxitimer::ITimer_Virtual ITIMER_VIRTUAL SIGVTALRM User-mode CPU time
cxxitimer::ITimer_Prof ITIMER_PROF SIGPROF Total (user + system) CPU time

Only one instance of each type may exist at any time. Attempting to create a second instance throws std::logic_error.

Usage

Creating a Timer

Intervals and initial values can be specified as timeval or double (seconds):

#include <cxxitimer.hpp>
// 1-second interval using timeval
cxxitimer::ITimer_Real timer({.tv_sec = 1, .tv_usec = 0});
// 0.5-second interval using double
// separate initial value and interval
cxxitimer::ITimer_Real timer(1.0, 2.0); // interval = 1s, first expiry after 2s
class ITimer_Real

Starting and Stopping

timer.start();
// ...
timer.stop();

Speed Control

The speed factor scales the timer interval. It can be changed while the timer is running.

timer.set_speed_factor(0.5); // half speed (slower)
timer.set_speed_factor(2.0); // double speed (faster)
timer.set_speed_to_normal(); // reset to 1.0

Changing the Interval

The timer must be stopped before changing the interval:

timer.set_interval(0.25); // set interval to 250 ms

Serialization

Timer state (interval and value) can be written to and read from binary file streams:

std::ofstream ofs("timer.bin", std::ios::binary);
timer.to_fstream(ofs);
std::ifstream ifs("timer.bin", std::ios::binary);
timer.from_fstream(ifs);

Helper Functions

The library also provides arithmetic operators for timeval and itimerval (multiplication and division by a double factor), as well as conversion functions:

Repository

Project URL: https://gitlab.com/nikolask-cpp-libs/cxxitimer

License

This library is licensed under the MIT License.

Copyright (c) 2022-2026 Nikolas Koesling