QTimer Accuracy for Qt::PreciseTimer
-
Hello, I used a QTimer to fetch real-time data. I know that the resolution is 1ms for
Qt::PreciseTimer
, and I just test this on my machine.connect(mytimer, &QTimer::timeout, this, &MyClass::onTimeOut); mytimer->setInterval(20); mytimer->setTimerType(Qt::PreciseTimer); mytimer->start();
For Windows machine , I use
QueryPerformanceFrequency
andQueryPerformanceCounter
to record time intervals. And for Linux machine, I usegettimeofday
.I draw a plot for the recorded time intervals as the following plot. The y axis is the time, and the unit is
ms
. The x axis is the number of time intervals. For most time, I can see the time interval falls between 19ms and 21ms.
But it exceeds 21ms or runs below 19ms sometimes, I was wondering, if someone would give me a hint about why this occurs?The above plot is the test on Windows. But I could observe similar thing on Linux machine. Could someone tell me why QTimer would be not accurate sometimes? Is this related to system's task dispatch rules or anything else?
Thank you!!!
-
@Pandako said in QTimer Accuracy for Qt::PreciseTimer:
But it exceeds 21ms or runs below 19ms sometimes, I was wondering, if someone would give me a hint about why this occurs?
This is called "jitter".
Windows is definitely not a real-time operating system (RTOS), and most Linux machines aren't either. This means the OS does not guarantee that your timers will always meet the intervals that you specify.
If you truly need deterministic timers which guarantee that jitter levels are always below a certain threshold, then you need an RTOS.
- Qt officially supports a few commercial RTOS'es: INTEGRITY, VxWorks, and QNX
- Ubuntu also recently announced a beta version of a real-time kernel: https://ubuntu.com/engage/an-introduction-to-real-time-linux-part-i