【问题标题】:Qthread - sleep till eventQthread - 睡眠直到事件
【发布时间】:2016-06-29 15:00:03
【问题描述】:

我想要一个只要在我的程序中触发信号就休眠的线程。 我创建了一个 Qthread 并将我的类添加到其中

QThread* serialthread = new QThread;
Serial*  serial = new Serial();
serial->moveToThread(serialthread);
connect(serial, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
connect(serialthread, SIGNAL(started()), serial, SLOT(process));
connect(serial, SIGNAL(finished()), serialthread, SLOT(quit()));
connect(serial, SIGNAL(finished()), serial, SLOT(deleteLater()));
connect(serialthread, SIGNAL(finished()), serialthread, SLOT(deleteLater()));
serialthread->start();

我类的函数serial::sendRequest() 应该由信号触发。 另一次我希望线程在不浪费 CPU 时间的情况下休眠。

【问题讨论】:

  • 只需将 slot sendRequest() 连接到一个信号就可以了。您的具体问题是什么?
  • 我的问题是不知道 qthread::run 启动了一个不会烧毁 CPU 的事件循环。但现在我知道了:)

标签: c++ multithreading qt qthread


【解决方案1】:

线程已经处于休眠状态,并且在没有工作要做时不会浪费任何 CPU 时间。你不需要做任何特别的事情来获得这种行为。这就是QThread::run 的默认实现的行为方式:它启动一个事件循环。当事件循环没有要处理的事件时,它会休眠,等待更多事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-06
    • 2011-10-06
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    相关资源
    最近更新 更多