title: How would you implement a basic event-loop?
body: <p>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():</p>
  
  <pre><code>int main() {
      QApplication app(argc, argv);
      // init code
      return app.exec();
  }
  </code></pre>
  
  <p>Which in this case, app.exec() is the application's main-loop.</p>
  
  <p>The obvious way to implement this kind of loop would be:</p>
  
  <pre><code>void exec() {
      while (1) {
          process_events(); // create a thread for each new event (possibly?)
      }
  }
  </code></pre>
  
  <p>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?</p>
  
  <p>Answers are appreciated in Python and/or C++.  Thanks.</p>
  
  <p>Footnote:  For the sake of learning, I will implement my own signals/slots, and I would use those to generate custom events (e.g. <code>go_forward_event(steps)</code>).  But if you know how I can use system events manually, I would like to know about that too.</p>