Package: glib

CStruct g-main-loop

Details

The main event loop manages all the available sources of events for GLib and GTK applications. These events can come from any number of different types of sources such as file descriptors (plain files, pipes or sockets) and timeouts. New types of event sources can also be added using the g-source-attach function.

To allow multiple independent sets of sources to be handled in different threads, each source is associated with a g-main-context instance. A g-main-context instance can only be running in a single thread, but sources can be added to it and removed from it from other threads. All functions which operate on a g-main-context instance or a built-in g-source instance are thread-safe.

Each event source is assigned a priority. The +g-priority-default+ default priority, is 0. Values less than 0 denote higher priorities. Values greater than 0 denote lower priorities. Events from high priority sources are always processed before events from lower priority sources.

Idle functions can also be added, and assigned a priority. These will be run whenever no events with a higher priority are ready to be processed.

The g-main-loop data type represents a main event loop. A g-main-loop instance is created with the g-main-loop-new function. After adding the initial event sources, the g-main-loop-run function is called. This continuously checks for new events from each of the event sources and dispatches them. Finally, the processing of an event from one of the sources leads to a call to the g-main-loop-quit funcion to exit the main loop, and the g-main-loop-run function returns.

It is possible to create new instances of g-main-loop instances recursively. This is often used in GTK applications when showing modal dialog boxes. Note that event sources are associated with a particular g-main-context instance, and will be checked and dispatched for all main loops associated with that g-main-context instance.

GTK contains wrappers of some of these functions, e.g. the gtk-main, gtk-main-quit and gtk-events-pending functions.
 

See also

2021-12-10