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

  • SEARCH
  • Home
  • 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

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Perhaps: <servlet> <servlet-name>HelloExcel</servlet-name> <servlet-class>writeDataBase.readDataBase</servlet-class> </servlet> <servlet> <servlet-name>HelloWord</servlet-name> <servlet-class>writeDataBase.writeDataBase</servlet-class> </servlet> <servlet-mapping>… May 12, 2026 at 1:12 am
  • Editorial Team
    Editorial Team added an answer I figured out this issue. I used IJavaElement instead of… May 12, 2026 at 1:12 am
  • Editorial Team
    Editorial Team added an answer No that's not true. Performance is good since all it… May 12, 2026 at 1:12 am

Related Questions

In the past I've worked with a number of programmers who have worked exclusively
I have a Windows Service written in Delphi which runs a number of programs.
I wrote a small application to use as a sandbox for testing ideas that
If you have worked with DotNetNuke, what are advantages and disadvantages that you have

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.