Results 1 to 4 of 4

Thread: Strange QTimer behaviour in multithreaded system

  1. #1
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded

    Default Strange QTimer behaviour in multithreaded system

    Hi,

    I found an interessting behaviour in QTimer that I believe is in contradiction to what the documentation says. It seems like QTimer is racing to make up for lost timer ticks instead of discarding them.

    My system has 2 threads. Thread 1 is responsible for the communication and uses a 100ms timer to send and receive data from a microcontroller. Thread 2 does the heavy processing that take up to several seconds. If now Thread 2 uses all CPU time for it's processing Thread 1 should afterwards continue to send/receive every 100ms even if some timer ticks were lost due to the CPU being unavailable. This is also what the documentation of QTimer says:

    "Timers will never time out earlier [...].
    If Qt is unable to deliver the requested number of timer clicks, it will silently discard some."

    On my system this is not the case. I am seeing timer ticks emitted every 20-30ms for some time after the heavy processing until the rate stabilizes again at the nominal 100ms. It seems like QTimer is trying to make up for the lost timer ticks.

    I found a workaround though that I can recommend if anybody has a similar issue. Instead of using a QTimer I am now using a forever-loop that calls the send/receive method and uses msleep() for the 100ms delay. On my embedded linux platform that works.

    What is your take on this? Did I miss any configuration options on QTimer? Anybody had the same problem before?

  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: Strange QTimer behaviour in multithreaded system

    How do you know what is the real interval of timer timeouts? Where are you "seeing" those ticks?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded

    Default Re: Strange QTimer behaviour in multithreaded system

    Quote Originally Posted by wysota View Post
    How do you know what is the real interval of timer timeouts? Where are you "seeing" those ticks?
    I started seeing it because the microcontroller that I am communicating with started to complain about timing violations. To verify it I am using the output of a separate QTime.

    It looks like this:

    Qt Code:
    1. QTime *debug_time; //used for measuring elapsed time between two ticks
    2. QTimer *tx_timer; //used to trigger the periodic send/receive
    3.  
    4. void TxThread::run() {
    5. debug_time->start();
    6. tx_timer = new QTimer();
    7. tx_timer->setInterval(SEND_INTERVAL);
    8. connect(tx_timer, SIGNAL(timeout()), this, SLOT(send_receive()));
    9. tx_timer->start();
    10. exec();
    11. }
    12.  
    13. void TxThread::send_receive() {
    14. qDebug() << "SR: " << debug_time->elapsed() << "ms";
    15. //here comes the actual TX/RX part via SPI
    16. }
    To copy to clipboard, switch view to plain text mode 

  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: Strange QTimer behaviour in multithreaded system

    But you are not measuring timer intervals but rather slot call intervals which are done across threads in your case. The timer is happily ticking at 100ms but since the other thread does heavy calculations, there is a buildup of pending slot calls that are executed independent of when the timer ticks.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    sysmaniac (22nd August 2011)

Similar Threads

  1. strange behaviour with Qt key_space
    By franco.amato in forum Qt Programming
    Replies: 0
    Last Post: 9th April 2010, 23:21
  2. QMainWindow - strange behaviour
    By franco.amato in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2009, 23:44
  3. Strange QGridLayout behaviour
    By Mat12345 in forum Qt Programming
    Replies: 0
    Last Post: 7th November 2009, 10:48
  4. Need help: Strange behaviour
    By navi1084 in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2008, 04:03
  5. very strange behaviour
    By regix in forum Qt Programming
    Replies: 23
    Last Post: 20th July 2006, 17:38

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.