Qthread - 睡眠直到事件

Qthread - sleep till event

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

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();

我的 class 的函数 serial::sendRequest() 应该由信号触发。 另一次我想让线程休眠而不浪费 CPU-Time.

线程已经在休眠,并且在没有工作可做时不会浪费任何 CPU 时间。您无需执行任何特殊操作即可获得该行为。这就是 QThread::run 的默认实现的行为方式:它启动一个事件循环。当事件循环没有事件要处理时,它会休眠,等待更多事件。