每当我运行一段QEventLoop
时间等待线程完成后台工作时,即使线程在其函数中进行了大量等待并且没有进行密集计算,CPU 使用率也会上升到 100%。它是加载 CPU 的事件循环。有没有办法配置QEventLoop
运行更慢并节省 CPU?
这是一个例子:
WorkerThread thread;
QEventLoop eventLoop;
QTimer threadTimer;
threadTimer.setSingleShot(true);
connect(&threadTimer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
connect(&thread, SIGNAL(finished()), &eventLoop, SLOT(quit()));
threadTimer.start(180000);
thread.start();
eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
if(threadTimer.isActive())
{
threadTimer.stop();
}