PDA

View Full Version : Isn't event loop going to take up lot of CPU time as it is basically a while loop?



activeLearner
18th May 2021, 19:18
From this (https://wiki.qt.io/Threads_Events_QObjects) official Qt documentation event loop conceptually executes as follows:



while (is_active) {
while (!event_queue_is_empty)
dispatch_next_event();
wait_for_more_events();
}

As one can observe that Qt application will utilise the CPU all the time as it needs to keep running the loop for its GUI to be updated. If this is the case what happens when multiple threads run concurrently in addition to GUI thread? Will the performance degrade while multi tasking?

ChristianEhrlicher
19th May 2021, 16:11
Did you see that a simple Qt application which simply runs an event loop take 100% of a CPU? Me not. So you assumption is wrong.

d_stranz
19th May 2021, 20:42
conceptually

This is the key word in that phrase. The GUI thread is like any other thread running in your PC's operating system. If it goes idle waiting for input, it will be suspended so that other active threads can run. The only way the Qt GUI thread will lock up your program (but not your PC) is if you implement such a loop (or use a blocking function) yourself and never let control return to the Qt event loop.