package lwt
- Overview
- No Docs
You can search for identifiers within the package.
in-package search v0.2.0
Install
dune-project
Dependency
Authors
Maintainers
Sources
md5=94272fac89c5bf21a89c102b8a8f35a5
sha512=8951b94555e930634375816d71815b9d85daad6ffb7dab24864661504d11be26575ab0b237196c54693efa372a9b69cdc1d5068a20a250dc0bbb4a3c03c5fda1
doc/lwt/Lwt_switch/index.html
Module Lwt_switch
Lwt switches
Switch has 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, consider 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.tNow you want to call f, g and h in parallel. You can simply do:
lwt idf = f () and idg = g () and idh = h () in
...However, one may want 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 if we change the interface a little bit:
val f : ?switch : Lwt_switch.t -> unit -> id Lwt.t
val g : ?switch : Lwt_switch.t -> unit -> id Lwt.t
val h : ?switch : Lwt_switch.t -> unit -> id Lwt.tthe code becomes:
Lwt_switch.with_switch (fun switch ->
lwt idf = f ~switch ()
and idg = g ~switch ()
and idh = h ~switch () in
...
)val create : unit -> tcreate () creates a new switch.
with_switch fn is fn switch, where switch is a fresh switch that is turned off when the callback thread finishes (whether it succeeds or fails).
val is_on : t -> boolis_on switch returns true if the switch is currently on, and false otherwise.
turn_off switch turns off the switch. It calls all registered hooks, waits for all of them to terminate, then returns. If one of the hooks failed, it will fail with the exception raised by the hook. If the switch is already off, it does nothing.
val check : t option -> unitcheck switch does nothing if switch is None or contains an switch that is currently on, and raises Off otherwise.