Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QThread: msleep doesn't work when system time is changed
Forum Updated to NodeBB v4.3 + New Features

QThread: msleep doesn't work when system time is changed

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CP71C Offline
    CP71C Offline
    CP71
    wrote on last edited by CP71
    #1

    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.6

    I’m wondering if is it a bug or normal behaviour?

    If it is normal behaviour how I can fix it without QTimer?

    Thanks
    CP71

    J.HilkJ 1 Reply Last reply
    0
    • CP71C 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.6

      I’m wondering if is it a bug or normal behaviour?

      If it is normal behaviour how I can fix it without QTimer?

      Thanks
      CP71

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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#details

      on how to properly do threading in Qt.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      CP71C 1 Reply Last reply
      2
      • J.HilkJ J.Hilk

        @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#details

        on how to properly do threading in Qt.

        CP71C Offline
        CP71C Offline
        CP71
        wrote on last edited by CP71
        #3

        @J.Hilk
        Thank you very much.
        It is very very interesting reading and I’ll take into consideration for future.

        1 Reply Last reply
        1
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          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?

          aha_1980A 1 Reply Last reply
          4
          • Kent-DorfmanK Kent-Dorfman

            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?

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Kent-Dorfman QElapsedTimer does, AFAIK.

            Qt has to stay free or it will die.

            1 Reply Last reply
            4
            • Kent-DorfmanK Offline
              Kent-DorfmanK Offline
              Kent-Dorfman
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved