2023-04-05

(sph thread-pool)

thread-pool that uses wait-conditions to pause unused threads and has a customisable queue type

part of sph-lib

module name

(sph thread-pool)

exported bindings

variable: sph-thread-pool-description
procedure: thread-pool-create [size exception-handler queue-type] ->
[integer procedure:{key retry ->} true/symbol/(symbol ...) list/symbol:lifo/fifo] -> (procedure:{procedure:nullary:code-to-execute -> any/false}:enqueue! thread ...)
creates a list of threads that wait using condition variables to execute procedures from a queue, which is a first-in-first-out queue by default.
the last-in-first-out queue just uses scheme lists and earlier tasks wait if there are not enough threads available to handle all tasks.
the returned enqueue! procedure can be used to add procedures to the queue.
if a procedure from the queue evaluates to false, the thread it was called in exits.
by adding as many procedures like this as there are threads in the pool,
all threads can be closed without having to cancel them by other more obscure means.
the default size is the current processor count.
queue-type can be a custom queue given as a list with the following signature
queue-type: (procedure:make-queue procedure:empty?:{queue -> boolean} procedure:enqueue:{queue a ->} procedure:dequeue:{queue -> any})
procedure: thread-pool-finish enqueue thread-list ->
(thread ...) -> (thread-exit-value ...)
let threads complete all currently enqueued tasks and exit