Results 1 to 4 of 4

Thread: QThread msleep precision

  1. #1
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Talking QThread msleep precision

    Hi,
    I have an application where I need to have precise (at millisecond) timing.
    I know I cannot trust QTimer, since the app is quite big and I think using Qtimer I accumulate delays.
    Let's say, I run a startTimer(5), if I check using QTime::currentTime(), I notice a delay around 10-15 ms instead of 5.
    So I tried adding a simple QThread to the app, just to see if in another thread I can be more precise.

    Qt Code:
    1. void testThread::run()
    2. {
    3. while (!m_stop)
    4. {
    5. m_currTime = QTime::currentTime();
    6. int elap = m_oldTime.msecsTo(m_currTime);
    7. m_oldTime = m_currTime;
    8. QString tt = QString("delay:%1").arg(elap);
    9. qDebug() << tt;
    10. msleep(10);
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    And even like this the delay is 15-16, sometimes 0 ! and if the app has some blocking operation even the thread is affected.

    The Cpu is always at 50%, so am I doing something wrong?
    Or that's normal? I would expect to have the thread runnig separatedly from the main process.

    thanks for any idea
    bye

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread msleep precision

    To me it looks like you have a two processor board and all threads of the process have the same CPU affinity resulting in one CPU being idle all the time. Anyway if you need precise timing, you need to use a real-time operating system (or at least its emulation like real-time priority scheduling in regular Linux). Otherwise you'll be lagging behind. Always.

    Your code looks fine, by the way. Maybe you are starting the thread in a wrong way? Did you call QThread::start() to start the thread?

  3. #3
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread msleep precision

    thanks for replying,
    of course I use Qthread::start.

    The thing if the thread is still affected by what's happening in the main process it seems useless, at least in my case.

    So I was thinking maybe should I test other thread like SDL....

    I'm building a video player and I need to check frequently the AV sync, every delay results in lag while playing the video.

    bye

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread msleep precision

    You can check the sync while displaying a frame, you don't need threads for that. Keep in mind that threads slow down your application, contrary to a common belief of being otherwise.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.