Event loop: Libvirt GLib Reference Manual

## Event loop Event loop — Integrate libvirt with the GMain event framework |

## Stability Level

Stable, unless otherwise indicated

## Functions

void | gvir_event_register ()

## Includes

    #include <libvirt-glib/libvirt-glib.h>

## Description

The libvirt API has the ability to provide applications with asynchronous notifications of interesting events. To enable this functionality though, applications must provide libvirt with an event loop implementation. The libvirt-glib API provides such an implementation, which naturally integrates with the GMain event loop framework.

To enable use of the GMain event loop glue, the ``gvir_event_register()`` should be invoked. Once this is done, it is mandatory to have the default GMain event loop run by a thread in the application, usually the primary thread, eg by using ``gtk_main()`` or ``g_application_run()``

Example 3. Registering for events with a GTK application

    1
    2
    3
    4
    5
    6
    7
    int main(int argc, char **argv) {
      ...setup...
      gvir_event_register();
      ...more setup...
      gtk_main();
      return 0;
    }

Example 4. Registering for events using Appplication

    1
    2
    3
    4
    5
    6
    7
    8
    int main(int argc, char **argv) {
      ...setup...
      GApplication *app = ...create some impl of GApplication...
      gvir_event_register();
      ...more setup...
      g_application_run(app);
      return 0;
    }

## Functions

### gvir_event_register ()

    void
    gvir_event_register (void);

Registers a libvirt event loop implementation that is backed by the default `GMain` context. If invoked more than once this method will be a no-op. Applications should, however, take care not to register any another non-GLib event loop with libvirt.

After invoking this method, it is mandatory to run the default GMain event loop. Typically this can be satisfied by invoking `gtk_main` or `g_application_run` in the application's main thread. Failure to run the event loop will mean no libvirt events get dispatched, and the libvirt keepalive timer will kill off libvirt connections frequently.

## Types and Values