I need a timer to execute callbacks with relatively low resolution. What’s the best way to implement such C++ timer class in Linux? Are there any libraries I could use?
I need a timer to execute callbacks with relatively low resolution. What’s the best
Share
If you’re writing within a framework (Glib, Qt, Wx, …), you’ll already have an event loop with timed callback functionalities. I’ll assume that’s not the case.
If you’re writing your own event loop, you can use the
gettimeofday/selectpair (struct timeval, microsecond precision) or theclock_gettime/nanosleeppair (struct timespec, nanosecond precision) for your own event dispatcher. Even though latter interface is higher resolution, scheduling is never that accurate anyways, so take whatever fits best.Warning: I love C. I never write C++. I’m just pretending to know the language.
Disclaimer: written just now and totally untested. The basic idea is to keep events in a priority queue, wait until the first one, run it, and repeat.