Lyricdev
GitHub

class Pool

pub class Pool :: [Closeable]

Conforms to Closeable.

A fixed set of threads that run the tasks given to the pool.

Pool.spawn runs a task on one of them — each in turn — and returns its handle; Pool.close waits until the tasks given to the pool have ended, then ends its threads. A pool is Closeable, so using let pool = Pool.new(4); closes it at the end of the scope.

Methods

pub static fn new(threads: int): Pool

Creates a pool of threads threads, each with a scheduler of its own.

A count below one makes a pool of one thread.

pub mut fn spawn<T, E :: [Error]>(body: fn() -> T throws E): (Task<T> throws E)

Runs body as a task on the pool's next thread in turn and returns its handle.

await() on the handle waits for the task from any thread.

Panics

Panics if the pool is closed.

pub fn close(): void throws Error

Waits until every task given to the pool has ended, then ends its threads.

Each thread ends as a Thread does: what those tasks started and left there is cancelled, and has ended when this returns. Closing a closed pool waits for nothing.

Errors

Throws Cancelled when the calling task is cancelled during the wait.