1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| class Thread : public QThread { public: Thread(QObject *parent = nullptr) : QThread(parent) {}
~Thread() { stopThread(); }
void startThread() { m_runing = true; if (isRunning()) { return; } start(); }
void stopThread() { m_runing = false; if (isRunning()) { m_waitCondition.wakeOne(); quit(); wait(); } }
protected: void run() { while (m_runing) { qDebug() << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss") << "start"; QMutexLocker locker(&m_mutex); m_waitCondition.wait(&m_mutex, QDeadlineTimer(30 * 1000)); qDebug() << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss") << "stop"; } }
private: volatile bool m_runing = false; QMutex m_mutex; QWaitCondition m_waitCondition; };
|