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. How to create a watchdog timer in Qt
Forum Updated to NodeBB v4.3 + New Features

How to create a watchdog timer in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtimer watchdog
13 Posts 7 Posters 9.4k Views 5 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.
  • M Offline
    M Offline
    mjsurette
    wrote on 14 May 2016, 17:48 last edited by
    #1

    I have a need for a watchdog timer, but can't figure out how to implement one.

    Any pointers or other help would be appreciated.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 14 May 2016, 18:07 last edited by
      #2

      hi
      What should the dog watch ?
      Can you say a little bit more about the function you are after?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mjsurette
        wrote on 14 May 2016, 20:09 last edited by
        #3

        I'm sorry, I thought that it was a common term. I guess not.

        A watchdog timer is one that counts down from a preset value. Incoming events can reset it to its original value and thereby preventing it from timing out. If there is an absence of triggers for long enough it times out and triggers an action.

        In my case, I have updates to some data that comes in bursts of indeterminate size. I would like to wait until there have been no updates for 10ms or so before updating the screen so they can all be handled in a batch. This would be much more efficient.

        I originally thought that a QTimer in singleshot mode would do the trick, but you can only trigger it once and there is no way to restart it.

        ? T M 3 Replies Last reply 14 May 2016, 20:18
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 14 May 2016, 20:14 last edited by
          #4

          Hi,

          QTimer is the right tool. Make one member of your class, configure it as single shot and you can then restart it as needed.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M mjsurette
            14 May 2016, 20:09

            I'm sorry, I thought that it was a common term. I guess not.

            A watchdog timer is one that counts down from a preset value. Incoming events can reset it to its original value and thereby preventing it from timing out. If there is an absence of triggers for long enough it times out and triggers an action.

            In my case, I have updates to some data that comes in bursts of indeterminate size. I would like to wait until there have been no updates for 10ms or so before updating the screen so they can all be handled in a batch. This would be much more efficient.

            I originally thought that a QTimer in singleshot mode would do the trick, but you can only trigger it once and there is no way to restart it.

            ? Offline
            ? Offline
            A Former User
            wrote on 14 May 2016, 20:18 last edited by
            #5

            @mjsurette said:

            there is no way to restart it

            You can restart the timer by calling [slot] void QTimer::start() again.

            1 Reply Last reply
            1
            • M Offline
              M Offline
              mjsurette
              wrote on 14 May 2016, 21:12 last edited by
              #6

              I must be missing something.

              This simple test program should have two timeouts, at 6 and 16 seconds in. But only one registers, and that's at the close of the program.

              #include <QCoreApplication>
              #include <QTimer>
              #include <QDebug>
              #include <QDateTime>
              
              #include <unistd.h>
              
              qint64 starttime;
              
              void timeout()
              {
                  qDebug() << "timeout:" << QDateTime::currentMSecsSinceEpoch()-starttime;
              }
              
              int main(int argc, char *argv[])
              {
                  QCoreApplication a(argc, argv);
              
                  QTimer wd;
                  wd.setSingleShot(true);
                  QObject::connect(&wd,&QTimer::timeout,&timeout);
              
                  starttime= QDateTime::currentMSecsSinceEpoch();
              
                  qDebug() << "start:" << QDateTime::currentMSecsSinceEpoch()-starttime;
              
                  for (int i=0;i<20;++i)
                  {
                      qDebug() << i;
                      if (i==1 || i==11)
                          wd.start(5000);
                      sleep(1);
                  }
                  qDebug() << "done:" << QDateTime::currentMSecsSinceEpoch()-starttime;
                  return a.exec();
              }
              
              
              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 14 May 2016, 21:20 last edited by
                #7

                The event loop is not running while you're in that for loop so your timer will only run once.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on 14 May 2016, 21:21 last edited by
                  #8

                  I must be missing something.

                  Timer events are processed in the event loop (exec() call). You restart the timer 20 times before the loop even starts, so only the last one actually is processed (emits a timeout() signal).

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mjsurette
                    wrote on 14 May 2016, 21:34 last edited by
                    #9

                    Thanks for the explanation. I'll refactor my test program.

                    kshegunovK 1 Reply Last reply 15 May 2016, 07:41
                    0
                    • M mjsurette
                      14 May 2016, 21:34

                      Thanks for the explanation. I'll refactor my test program.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on 15 May 2016, 07:41 last edited by kshegunov
                      #10

                      @mjsurette said:

                      In my case, I have updates to some data that comes in bursts of indeterminate size. I would like to wait until there have been no updates for 10ms or so before updating the screen so they can all be handled in a batch. This would be much more efficient.

                      If I understand correctly you want to block event processing for a given amount of time, which doesn't exactly play well with QTimer. You can have a timed wait on a locked synchronization primitive for that:

                      QSemaphore lock(0);
                      lock.tryAcquire(1, 1000);  // Wait 1s for the semaphore to be released.
                      

                      Note: If I've misunderstood, and you want not to block the event loop, then this is certainly not the solution.

                      PS. Another way for a blocking wait with event processing is by using a local event loop:

                      QObject * emitter; //< This is the object that will emit a signal when something happened and the wait should interrupt
                      
                      QEventLoop loop;
                      QObject::connect(emitter, SIGNAL(interruptWait()), &loop, SLOT(quit()));
                      loop.exec();  // Wait until interruptWait() has been called
                      

                      Kind regards.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • M mjsurette
                        14 May 2016, 20:09

                        I'm sorry, I thought that it was a common term. I guess not.

                        A watchdog timer is one that counts down from a preset value. Incoming events can reset it to its original value and thereby preventing it from timing out. If there is an absence of triggers for long enough it times out and triggers an action.

                        In my case, I have updates to some data that comes in bursts of indeterminate size. I would like to wait until there have been no updates for 10ms or so before updating the screen so they can all be handled in a batch. This would be much more efficient.

                        I originally thought that a QTimer in singleshot mode would do the trick, but you can only trigger it once and there is no way to restart it.

                        T Offline
                        T Offline
                        t3685
                        wrote on 15 May 2016, 08:17 last edited by
                        #11

                        @mjsurette

                        Just making sure you keep in mind the inherit limitations of qtimer. Qt is not a real time system. See the documentation http://doc.qt.io/qt-5/qtimer.html for more information.

                        An alternative to batching by time, is to simply batch by size. It will be easier to write and the behaviour will probably be more consistent.

                        kshegunovK 1 Reply Last reply 15 May 2016, 08:48
                        1
                        • T t3685
                          15 May 2016, 08:17

                          @mjsurette

                          Just making sure you keep in mind the inherit limitations of qtimer. Qt is not a real time system. See the documentation http://doc.qt.io/qt-5/qtimer.html for more information.

                          An alternative to batching by time, is to simply batch by size. It will be easier to write and the behaviour will probably be more consistent.

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on 15 May 2016, 08:48 last edited by kshegunov
                          #12

                          @t3685 said:

                          Qt is not a real time system. See the documentation http://doc.qt.io/qt-5/qtimer.html for more information.

                          Nothing to do with Qt. It's the limitation of the underlying OS you have to take in mind.

                          Read and abide by the Qt Code of Conduct

                          1 Reply Last reply
                          0
                          • M mjsurette
                            14 May 2016, 20:09

                            I'm sorry, I thought that it was a common term. I guess not.

                            A watchdog timer is one that counts down from a preset value. Incoming events can reset it to its original value and thereby preventing it from timing out. If there is an absence of triggers for long enough it times out and triggers an action.

                            In my case, I have updates to some data that comes in bursts of indeterminate size. I would like to wait until there have been no updates for 10ms or so before updating the screen so they can all be handled in a batch. This would be much more efficient.

                            I originally thought that a QTimer in singleshot mode would do the trick, but you can only trigger it once and there is no way to restart it.

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 15 May 2016, 09:39 last edited by
                            #13

                            @mjsurette
                            Just a note:
                            WatchDog -is- a common term, i just wanted to hear what your overall
                            goal was with it
                            as there are many way to program a watchdog. :)

                            1 Reply Last reply
                            0

                            1/13

                            14 May 2016, 17:48

                            • Login

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