【发布时间】:2011-10-06 00:48:16
【问题描述】:
如何在 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();
}
【问题讨论】: