Key Concept:
Event-Driven Programming
The Hollywood Principle
Event Loop & Inversion of Control
Event-driven systems are naturally divided into the application, which actually handles the events, and the supervisory event-driven infrastructure (framework), which generically waits for events and dispatches them to the application.
The event-driven infrastructure is typically organized as an event-loop also known as “message loop” or “message pump”:
while (1) { /* "event-loop" (infrastructure) */ Event *e = getEvent(); /* BLOCKING */ dispatch(e); /* NO-BLOCKING (application) */ }
The event-loop is the only place where the program waits (blocks) for events. The event-driven application is not allowed to block, because it must quickly return control after handling each event back to the event loop.
The control resides in the event-driven infrastructure, so from the application standpoint the control is inverted compared to sequential programs, such as threads in a traditional RTOS.
This is the key characteristic of all event-driven systems and is the essence of event-driven programming. The inversion of control is really what it means that “events drive the application” and not the other way around.