Warning: Reason support is experimental. We are looking for beta-tester and contributors.

Module Lwt_glib

module Lwt_glib : sig..end

Glib integration


This module allow to use Lwt in GTK applications.

Here is what you have to do to make Lwt and GTK work together:

  • call Lwt_glib.install at the beginning of your program (before or after GMain.init, it does not matter)
  • do not call GMain.main, write your application as a normal Lwt application instead.

For example:

let () = Lwt_main.run (
        (* Initializes GTK. *)
        ignore (GMain.init ());

        (* Install Lwt<->Glib integration. *)
        Lwt_glib.install ();

        (* Thread which is wakeup when the main window is closed. *)
        let waiter, wakener = Lwt.wait () in

        (* Create a window. *)
        let window = GWindow.window () in

        (* Display something inside the window. *)
        ignore (GMisc.label ~text:"Hello, world!" ~packing:window#add ());

        (* Quit when the window is closed. *)
        ignore (window#connect#destroy (Lwt.wakeup wakener));

        (* Show the window. *)
        window#show ();

        (* Wait for it to be closed. *)
        waiter
      )
val install : ?mode:[ `glib_into_lwt | `lwt_into_glib ] -> unit -> unit

Install the Glib<->Lwt integration.

If mode is `glib_into_lwt then glib will use the Lwt main loop, and if mode is `lwt_into_glib then Lwt will use the Glib main loop.

mode defaults to `lwt_into_glib because it is more portable. `glib_into_lwt does not work under Windows and MacOS.

If the integration is already active, this function does nothing.

val remove : unit -> unit

Remove the Glib<->Lwt integration.

val iter : bool -> unit

This function is not related to Lwt. iter may_block does the same as Glib.Main.iteration may_block but can safely be called in a multi-threaded program, it will not block the whole program.

For example:

let main () =
          while true do
            Lwt_glib.iter true
          done

        let thread = Thread.create main ()

Note: you can call this function only from one thread at a time, otherwise it will raise Failure.

val wakeup : unit -> unit

If one thread is blocking on Lwt_glib.iter, then wakeup () make Lwt_glib.iter to return immediatly.