QThread: msleep doesn't work when system time is changed
-
Hi all,
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.
Void myThread::run()
{
do
{
_QTimer seems to work well….
msleep(1000);}while(mRun);
}
My OS is Linux
My Qt is 4.8.6I’m wondering if is it a bug or normal behaviour?
If it is normal behaviour how I can fix it without QTimer?
Thanks
CP71 -
Hi all,
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.
Void myThread::run()
{
do
{
_QTimer seems to work well….
msleep(1000);}while(mRun);
}
My OS is Linux
My Qt is 4.8.6I’m wondering if is it a bug or normal behaviour?
If it is normal behaviour how I can fix it without QTimer?
Thanks
CP71@CP71
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
take a look here
https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
and here
https://doc.qt.io/qt-5/qthread.html#detailson how to properly do threading in Qt.
-
@CP71
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
take a look here
https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
and here
https://doc.qt.io/qt-5/qthread.html#detailson how to properly do threading in Qt.
-
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?
-
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.