Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 167019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:12:02+00:00 2026-05-11T12:12:02+00:00

If you have worked with gui toolkits, you know that there is a event-loop/main-loop

  • 0

If you have worked with gui toolkits, you know that there is a event-loop/main-loop that should be executed after everything is done, and that will keep the application alive and responsive to different events. For example, for Qt, you would do this in main():

int main() {     QApplication app(argc, argv);     // init code     return app.exec(); } 

Which in this case, app.exec() is the application’s main-loop.

The obvious way to implement this kind of loop would be:

void exec() {     while (1) {         process_events(); // create a thread for each new event (possibly?)     } } 

But this caps the CPU to 100% and is practicaly useless. Now, how can I implement such an event loop that is responsive without eating the CPU altogether?

Answers are appreciated in Python and/or C++. Thanks.

Footnote: For the sake of learning, I will implement my own signals/slots, and I would use those to generate custom events (e.g. go_forward_event(steps)). But if you know how I can use system events manually, I would like to know about that too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:12:03+00:00Added an answer on May 11, 2026 at 12:12 pm

    I used to wonder a lot about the same!

    A GUI main loop looks like this, in pseudo-code:

    void App::exec() {     for(;;) {         vector<Waitable> waitables;         waitables.push_back(m_networkSocket);         waitables.push_back(m_xConnection);         waitables.push_back(m_globalTimer);         Waitable* whatHappened = System::waitOnAll(waitables);         switch(whatHappened) {             case &m_networkSocket: readAndDispatchNetworkEvent(); break;             case &m_xConnection: readAndDispatchGuiEvent(); break;             case &m_globalTimer: readAndDispatchTimerEvent(); break;         }     } } 

    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 vector<Waitable> is a ::fd_set on UNIX, and ‘whatHappened’ is actually queried via FD_ISSET. The actual waitable-handles are acquired in various ways, for example m_xConnection 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 simultaneously.

    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 all their time idling.

    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.

    Please ask for further details!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At every company I have worked at, I have found that people are still
If you have worked with DotNetNuke, what are advantages and disadvantages that you have
I have the main (GUI) thread which creates a QThread. In the QThread, I
I have worked for 5 years mainly in java desktop applications accessing Oracle databases
I have worked on projects for embedded systems in the past where we have
I have worked on iPhone application but not aware of the formalities to submit
I have worked a bit with Django and I quite like its project/applications model
I have worked on single threaded business logic/back-end programming for most of my career.
If any of you have worked with a cool tool for viewing/querying the SQL
A few years ago I have worked on a green field project where we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.