如何在 QThread 处于睡眠状态时唤醒它?
我有一个在后台运行的线程,然后不时醒来并做一些小事情,但是如果我想以受控方式停止该线程,我必须等待他自己醒来才能让他退出。而且由于他睡了很长时间,这可能很烦人。
这是一个显示基本问题的小示例代码。
让我们从这个例子中休眠 5 秒然后只打印一个点的线程开始。
#include <QDebug>
#include "TestThread.h"
void TestThread::run()
{
running = true;
while(running == true)
{
qDebug() << ".";
QThread::sleep(5);
}
qDebug() << "Exit";
}
void TestThread::stop()
{
running = false;
}
然后我们有 main 启动线程然后杀死他。
#include <QDebug>
#include "TestThread.h"
int main(int argc, char *argv[])
{
qDebug() << "Start test:";
TestThread *tt = new TestThread();
tt->start();
sleep(2);
tt->stop();
tt->wait();
delete tt;
}
问题是 tt->wait(); 必须等待线程休眠的 5 秒。我可以叫一个像“从睡眠中醒来”这样的东西,这样他就可以继续了。
还是有更好的方法来做到这一点?
/谢谢
更新我让它与 QMutex 和 tryLock 一起工作:
#include <QDebug>
#include "TestThread.h"
QMutex sleepMutex;
void TestThread::run()
{
qDebug() << "Begin";
//1. Start to lock
sleepMutex.lock();
//2. Then since it is locked, we can't lock it again
// so we timeout now and then.
while( !sleepMutex.tryLock(5000) )
{
qDebug() << ".";
}
//4. And then we cleanup and unlock the lock from tryLock.
sleepMutex.unlock();
qDebug() << "Exit";
}
void TestThread::stop()
{
//3. Then we unlock and allow the tryLock
// to lock it and doing so return true to the while
// so it stops.
sleepMutex.unlock();
}
但是使用 QWaitCondition 会更好吗?还是一样?
更新:如果启动和停止他的胎面不同,QMutex 就会中断,所以这里尝试使用 QWaitCondition。
#include <QDebug>
#include <QWaitCondition>
#include "TestThread.h"
QMutex sleepMutex;
void TestThread::run()
{
qDebug() << "Begin";
running = true;
sleepMutex.lock();
while( !waitcondition.wait(&sleepMutex, 5000) && running == true )
{
qDebug() << ".";
}
qDebug() << "Exit";
}
void TestThread::stop()
{
running = false;
waitcondition.wakeAll();
}