Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I used to wonder a lot about the same!</p> <p>A GUI main loop looks like this, in pseudo-code:</p> <pre><code>void App::exec() { for(;;) { vector&lt;Waitable&gt; waitables; waitables.push_back(m_networkSocket); waitables.push_back(m_xConnection); waitables.push_back(m_globalTimer); Waitable* whatHappened = System::waitOnAll(waitables); switch(whatHappened) { case &amp;m_networkSocket: readAndDispatchNetworkEvent(); break; case &amp;m_xConnection: readAndDispatchGuiEvent(); break; case &amp;m_globalTimer: readAndDispatchTimerEvent(); break; } } } </code></pre> <p>What is a "Waitable"? Well, it's system dependant. On UNIX it's called a "file descriptor" and "waitOnAll" is the ::select system call. The so-called <code>vector&lt;Waitable&gt;</code> is a <code>::fd_set</code> on UNIX, and "whatHappened" is actually queried via <code>FD_ISSET</code>. The actual waitable-handles are acquired in various ways, for example <code>m_xConnection</code> can be taken from ::XConnectionNumber(). X11 also provides a high-level, portable API for this -- ::XNextEvent() -- but if you were to use that, you wouldn't be able to wait on several event sources <em>simultaneously</em>.</p> <p>How does the blocking work? "waitOnAll" is a syscall that tells the OS to put your process on a "sleep list". This means you are not given any CPU time until an event occurs on one of the waitables. This, then, means your process is idle, consuming 0% CPU. When an event occurs, your process will briefly react to it and then return to idle state. GUI apps spend almost <em>all</em> their time idling.</p> <p>What happens to all the CPU cycles while you're sleeping? Depends. Sometimes another process will have a use for them. If not, your OS will busy-loop the CPU, or put it into temporary low-power mode, etc.</p> <p>Please ask for further details!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload