QWaitCondition deadline misunderstanding
-
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"
-
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(). -
@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.
-