Module Lwt_preemptive
This module allows to mix preemptive threads with Lwt cooperative threads. It maintains an extensible pool of preemptive threads to which you can detach computations.
See Mwt for a more modern implementation.
val detach : ('a -> 'b) -> 'a -> 'b Lwt.tdetach f xruns the computationf xin a separate preemptive thread.detachevaluates to an Lwt promise, which is pending until the preemptive thread completes.detachcallssimple_initinternally, which means that the number of preemptive threads is capped by default at four. If you would like a higher limit, callinitorset_boundsdirectly.Note that Lwt thread-local storage (i.e.,
Lwt.with_value) cannot be safely used from withinf. The same goes for most of the rest of Lwt. If you need to run an Lwt thread inf, userun_in_main.
val run_in_main : (unit -> 'a Lwt.t) -> 'arun_in_main fcan be called from a detached computation to executef ()in the main preemptive thread, i.e. the one executingLwt_main.run.run_in_main fblocks untilf ()completes, then returns its result. Iff ()raises an exception,run_in_main fraises the same exception.Lwt.with_valuemay be used insidef ().Lwt.getcan correctly retrieve values set this way insidef (), but not values set usingLwt.with_valueoutsidef ().
val init : int -> int -> (string -> unit) -> unitinit min max loginitialises this module. i.e. it launches the minimum number of preemptive threads and starts the dispatcher.- parameter min
is the minimum number of threads
- parameter max
is the maximum number of threads
- parameter log
is used to log error messages
If
Lwt_preemptivehas already been initialised, this call only modify bounds and the log function.
val simple_init : unit -> unitsimple_init ()checks if the library is not yet initialized, and if not, does a simple initialization. The minimum number of threads is set to zero, maximum to four, and the log function is left unchanged, i.e. the default built-in logging function is used. SeeLwt_preemptive.init.Note: this function is automatically called by
detach.
val get_bounds : unit -> int * intget_bounds ()returns the minimum and the maximum number of preemptive threads.
val set_bounds : (int * int) -> unitset_bounds (min, max)set the minimum and the maximum number of preemptive threads.
val set_max_number_of_threads_queued : int -> unitSets the size of the waiting queue, if no more preemptive threads are available. When the queue is full,
detachwill sleep until a thread is available.