Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. QWaitCondition deadline misunderstanding
Forum Update on Monday, May 27th 2025

QWaitCondition deadline misunderstanding

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 2 Posters 521 Views
  • 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.
  • Q Offline
    Q Offline
    qAlexKo
    wrote on last edited by qAlexKo
    #1

    Hi, all!

    The task is to see how QWaitCondition.wait(QMutext, QDeadlineTimer deadline) works. The test has two modes:
    In the first mode waitcondition.wait() has no deadline and it is waked by wakeAll() in the timer slot.
    In the second mode the timer is switched off, and I expect that the thread will still work because of the deadline, with the frequency defined by the deadline (1000 msec).
    But in the second variant the thread runs with maximum speed. Where is I wrong?

    #include <QCoreApplication>
    #include <QDebug>
    #include <QMutex>
    #include <QWaitCondition>
    #include <QThread>
    #include <QObject>
    #include <QDebug>
    #include <QTimer>
    
    bool DEADLINE_TST = true;   //true - turn on deadline tst
    
    QTimer tm_for_wakeking;
    QWaitCondition waitcondition;
    QMutex mutex;
    int count=0;
    void tm_for_wakeking_slot();
    
    //=================
    class thread1_tst : public QThread
    {
        Q_OBJECT
    public:
        explicit thread1_tst(QObject *parent = nullptr);
        void run();
    };
    //------------------------------
    thread1_tst::thread1_tst(QObject *parent)
        : QThread{parent}
    {}
    //------------------------------
    void thread1_tst::run()
    {
    bool res;  count=0;
    
      while(1)
      {
          mutex.lock();
          QDeadlineTimer deadline;
          deadline.setDeadline(1000);
          if(DEADLINE_TST)
             res = waitcondition.wait(&mutex, deadline);
          else
            res = waitcondition.wait(&mutex);
    
           if(res == false)   //it was wait deadline
               qDebug() << "thread: wait deadline heppened" << count++ << " DEADLINE_TST=" << DEADLINE_TST;
           else
               qDebug() << "mutex was unlock by event " << count++ << " DEADLINE_TST=" << DEADLINE_TST;
    
           mutex.unlock();
      }
    }
    //------------------------------
    thread1_tst thread1; 
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        thread1.start();
        if(DEADLINE_TST == false)
        {
          QObject::connect(&tm_for_wakeking, &QTimer::timeout, &a, &tm_for_wakeking_slot);
          tm_for_wakeking.start(250);
         }
        return a.exec();
    }
    //----------------------------
    void tm_for_wakeking_slot()
    {
        count++;
        waitcondition.wakeAll();   //wake waitcondition using timer
    }
    //----------------------------
    
    #include "waitconditions.moc"
    
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • Q qAlexKo

      Hi, all!

      The task is to see how QWaitCondition.wait(QMutext, QDeadlineTimer deadline) works. The test has two modes:
      In the first mode waitcondition.wait() has no deadline and it is waked by wakeAll() in the timer slot.
      In the second mode the timer is switched off, and I expect that the thread will still work because of the deadline, with the frequency defined by the deadline (1000 msec).
      But in the second variant the thread runs with maximum speed. Where is I wrong?

      #include <QCoreApplication>
      #include <QDebug>
      #include <QMutex>
      #include <QWaitCondition>
      #include <QThread>
      #include <QObject>
      #include <QDebug>
      #include <QTimer>
      
      bool DEADLINE_TST = true;   //true - turn on deadline tst
      
      QTimer tm_for_wakeking;
      QWaitCondition waitcondition;
      QMutex mutex;
      int count=0;
      void tm_for_wakeking_slot();
      
      //=================
      class thread1_tst : public QThread
      {
          Q_OBJECT
      public:
          explicit thread1_tst(QObject *parent = nullptr);
          void run();
      };
      //------------------------------
      thread1_tst::thread1_tst(QObject *parent)
          : QThread{parent}
      {}
      //------------------------------
      void thread1_tst::run()
      {
      bool res;  count=0;
      
        while(1)
        {
            mutex.lock();
            QDeadlineTimer deadline;
            deadline.setDeadline(1000);
            if(DEADLINE_TST)
               res = waitcondition.wait(&mutex, deadline);
            else
              res = waitcondition.wait(&mutex);
      
             if(res == false)   //it was wait deadline
                 qDebug() << "thread: wait deadline heppened" << count++ << " DEADLINE_TST=" << DEADLINE_TST;
             else
                 qDebug() << "mutex was unlock by event " << count++ << " DEADLINE_TST=" << DEADLINE_TST;
      
             mutex.unlock();
        }
      }
      //------------------------------
      thread1_tst thread1; 
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          thread1.start();
          if(DEADLINE_TST == false)
          {
            QObject::connect(&tm_for_wakeking, &QTimer::timeout, &a, &tm_for_wakeking_slot);
            tm_for_wakeking.start(250);
           }
          return a.exec();
      }
      //----------------------------
      void tm_for_wakeking_slot()
      {
          count++;
          waitcondition.wakeAll();   //wake waitcondition using timer
      }
      //----------------------------
      
      #include "waitconditions.moc"
      
      
      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You use the wrong setter, see QDeadlineTimer::setDeadline(): Sets the deadline for this QDeadlineTimer object to be the msecs absolute time point, counted in milliseconds since the reference clock.

      I think you want QDeadlineTimer deadline(1000); or QDeadlineTimer::setRemainingTime().

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Q 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        You use the wrong setter, see QDeadlineTimer::setDeadline(): Sets the deadline for this QDeadlineTimer object to be the msecs absolute time point, counted in milliseconds since the reference clock.

        I think you want QDeadlineTimer deadline(1000); or QDeadlineTimer::setRemainingTime().

        Q Offline
        Q Offline
        qAlexKo
        wrote on last edited by
        #3

        @Christian-Ehrlicher said in QWaitCondition deadline misunderstanding:

        You use the wrong setter, see QDeadlineTimer::setDeadline(): Sets the deadline for this QDeadlineTimer object to be the msecs absolute time point, counted in milliseconds since the reference clock.
        I think you want QDeadlineTimer deadline(1000); or QDeadlineTimer::setRemainingTime().

        Yes, everything is as you said, thank you.

        1 Reply Last reply
        1
        • Q qAlexKo has marked this topic as solved on

        • Login

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