C++ Qthread-休眠到事件

C++ Qthread-休眠到事件,c++,multithreading,qt,qthread,C++,Multithreading,Qt,Qthread,我想要一个线程,只要在我的程序中触发一个信号,它就会休眠。 我创建了一个Qthread并将我的类添加到其中 QThread* serialthread = new QThread; Serial* serial = new Serial(); serial->moveToThread(serialthread); connect(serial, SIGNAL(error(QString)), this, SLOT(errorString(QString))); connect(seria

我想要一个线程,只要在我的程序中触发一个信号,它就会休眠。 我创建了一个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时间的情况下睡眠。

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

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