A pool of threads with a worker-stealing scheduler. The pool contains a fixed number of worker threads that wait for work items to come, process these, and loop.
This is good for CPU-intensive tasks that feature a lot of small tasks. Note that tasks will not always be processed in the order they are scheduled, so this is not great for workloads where the latency of individual tasks matter (for that see Fifo_pool).
If a runner is no longer needed, shutdown can be used to signal all worker 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).
run_wait_block pool f schedules f for later execution on the pool, like run_async. It then blocks the current thread until f() is done executing, and returns its result. If f() raises an exception, then run_wait_block pool f will raise it as well.
NOTE be careful with deadlocks (see notes in Fut.wait_block about the required discipline to avoid deadlocks).
called at the beginning of each new thread in the pool.
parameternum_threads
size of the pool, ie. number of worker threads. It will be at least 1 internally, so 0 or negative values make no sense. The default is Domain.recommended_domain_count(), ie one worker thread per CPU core.
Note that specifying num_threads=n means that the degree of parallelism is at most n. This behavior is different than the one of Domainslib, see https://github.com/c-cube/moonpool/issues/41 for context.
If you want to use all cores, use Domain.recommended_domain_count().
parameteron_exit_thread
called at the end of each thread in the pool
parametername
a name for this thread pool, used if tracing is enabled (since 0.6)