1

Whenever I run a QEventLoop while waiting for a thread to finish background work, CPU usage goes up to 100% even if the thread does a lot of waiting in its function and doesn't do intense calculations. It's the event loop that loads the CPU. Is there a way to configure QEventLoop to run more slowly and spare the CPU?

Here is an example:

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();
}
ulak blade
  • 2,515
  • 5
  • 37
  • 81
  • 2
    Could you provide a working example of such behaviour? – Bowdzone Jan 26 '16 at 14:17
  • @Bowdzone I added an example – ulak blade Jan 26 '16 at 14:21
  • I do not really understand why you need an additional event loop and not just let the program run while the thread finishes its work in the background like it is supposed to – Bowdzone Jan 26 '16 at 14:24
  • What platform are you running on? You might need to set the thread priority in `thread.start()`, there's a list here: http://doc.qt.io/qt-5/qthread.html#Priority-enum – Nicholas Smith Jan 26 '16 at 14:24
  • @Bowdzone it is essential that the thread does something before the program can go on, but the program needs to be responsive until the thread finishes – ulak blade Jan 26 '16 at 14:33
  • The code you've provided doesn't reproduce the problem. If you claim that the event loop introduces the problem, you should demonstrate it with a `QThread` that does nothing. That's your claim, after all. – Kuba hasn't forgotten Monica Jan 26 '16 at 15:23
  • @KubaOber could the issue be that I haven't specified Qt::QueuedConnection in the connection calls? – ulak blade Jan 26 '16 at 16:58
  • That's not necessary anyway, because: 1. Automatic connection type is the default and does the right thing. 2. You're connecting between objects that live in the same thread anyway. – Kuba hasn't forgotten Monica Jan 26 '16 at 17:21

0 Answers0