Module Lwt_switch

module Lwt_switch : sig..end

Lwt switches

Switch have two goals:

  • being able to free multiple resources at the same time,
  • offer a better alternative than always returning an id to free some resource.

For example, considers the following interface:

type id

      val free : id -> unit Lwt.t

      val f : unit -> id Lwt.t
      val g : unit -> id Lwt.t
      val h : unit -> id Lwt.t

Now you want to calls f, g and h in parallel. You can simply do:

lwt idf = f () and idg = g () and idh = h () in

However, one may wants to handle possible failures of f (), g () and h (), and disable all allocated resources if one of these three threads fails. This may be hard since you have to remember which one failed and which one returned correctly.

Now we change a little bit the interface: