QThread: msleep doesn't work when system time is changed
I have an issue with msleep in QThread if system time is changed (e.g. from 2019/02/21 12:55 to 2019/02/21 11:55, for example by automatic datetime update via internet) while thread is sleeping by msleep.
msleep stays asleep for 1 hour in the above case.
_QTimer seems to work well….
My OS is Linux
My Qt is 4.8.6
I’m wondering if is it a bug or normal behaviour?
If it is normal behaviour how I can fix it without QTimer?
I'm going out on a limb here, but I would say with 99% certainty, you're not programming for a realtime operating system.
So do not use QThread::sleep
on how to properly do threading in Qt.
Thank you very much.
It is very very interesting reading and I’ll take into consideration for future.
To directly answer your question: it is the expected bevahiour because sleep functions look at the delta between when they were called and the expected wakeup timepoint. any change to the system clock will most likely have bad consequences. Kernel level sleep syscalls will exhibit this behaviour in any language. The low-level work around is to use one of the HPET (high-precision event timers), as they are not dependent upon the jiffy clock. Don't know for certain, but perhaps the QTimer uses them?
@Kent-Dorfman QElapsedTimer does, AFAIK.
Yes, without investigating I would expect the model for the Qt framework to set an HPET to a very fast period rate, and hook it into the event loop such that on each pass the its counter value can be examined and any expired timer events may then be dispatched.