Queue Example: Event Handling
One popular application of queues is handling events in event-driven applications. Event-driven applications execute largely under the direction of real-time occurrences called events. In a graphical user interface developed in Java, X, or Windows, for example, the behavior of an application depends a great deal on key presses, mouse movements, and other events triggered by the user. Other examples of event-driven applications occur frequently in control systems such as those found in aircraft or factory equipment.
In nearly all event-driven applications, events can occur at any moment, so queues play an important role in storing events until an application is ready to deal with them. A queue works well for this because applications handle events more or less in the same order as they occur.
Example 6.5 presents two
functions for handling events: receive_event
and process_event . Both functions operate on a queue containing events of
type Event.
Event is defined in event.h, which is not shown. An application
calls receive_event to enqueue an event it has been notified about. Exactly how an application
is notified of an event varies, but notification often begins with a
hardware interrupt. When the application decides it is time to process
an event, it calls process_event. Inside of
process_event, an event is dequeued from the event queue and is passed to an
application-specific dispatch function. The dispatch function is passed to
process_event as ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access