9

如何在 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();
}
4

2 回答 2

7

您可以使用 aQWaitCondition而不是简单的sleep. 如果给你更多的控制权。

此处的示例用法:等待条件示例

于 2011-07-15T10:08:09.847 回答
1

我认为不存在可移植的解决方案(尽管某些操作系统中可能有一些设施,例如 POSIX 信号)。无论如何,Qt 本身并没有提供这样的方法,因此你可以像这样模拟它

void TestThread::run()
{
    running = true;
    while(running == true)
    {
        qDebug() << ".";
        // Quantize the sleep:
        for (int i = 0; i < 5 && running; ++i) QThread::sleep(1);
    }
    qDebug() << "Exit";
}

但正如 Mat 所指出的,最好的解决方案仍然是 QWaitCondition。

于 2011-07-15T10:11:19.850 回答