package moonpool

  1. Overview
  2. Docs

Thread pool.

type t

A pool of threads. The pool contains a fixed number of threads that wait for work items to come, process these, and loop.

If a pool is no longer needed, shutdown can be used to signal all threads in it to stop (after they finish their work), and wait for them to stop.

The threads are distributed across a fixed domain pool (whose size is determined by Domain.recommended_domain_count on OCaml 5, and simple the single runtime on OCaml 4).

type thread_loop_wrapper = thread:Thread.t -> pool:t -> (unit -> unit) -> unit -> unit

a thread wrapper f takes the current thread, the current pool, and the worker function loop : unit -> unit which is the worker's main loop, and returns a new loop function. By default it just returns the same loop function but it can be used to install tracing, effect handlers, etc.

val add_global_thread_loop_wrapper : thread_loop_wrapper -> unit

add_global_thread_loop_wrapper f installs f to be installed in every new pool worker thread, for all existing pools, and all new pools created with create. These wrappers accumulate: they all apply, but their order is not specified.

val create : ?on_init_thread:(dom_id:int -> t_id:int -> unit -> unit) -> ?on_exit_thread:(dom_id:int -> t_id:int -> unit -> unit) -> ?thread_wrappers:thread_loop_wrapper list -> ?on_exn:(exn -> Printexc.raw_backtrace -> unit) -> ?min:int -> ?per_domain:int -> unit -> t

create () makes a new thread pool.

  • parameter on_init_thread

    called at the beginning of each new thread in the pool.

  • parameter on_exit_thread

    called at the end of each thread in the pool

  • parameter thread_wrappers

    a list of thread_loop_wrapper functions to use for this pool's workers.

val size : t -> int

Number of threads

val shutdown : t -> unit

Shutdown the pool and wait for it to terminate. Idempotent.

exception Shutdown
val run : t -> (unit -> unit) -> unit

run pool f schedules f for later execution on the pool in one of the threads. f() will run on one of the pool's worker threads.

  • raises Shutdown

    if the pool was shut down before run was called.

OCaml

Innovation. Community. Security.