Skip to content

Thread Pool

Spice
import "std/os/thread-pool";

ThreadPool struct

A thread pool that can be used to run multiple jobs in parallel. Thread pools in Spice work with a fixed number of worker threads, that are created when the pool is started. After that, an arbitrary number of jobs can be queued to be run by the pool. The pool will then run as many jobs as possible in parallel.

Constructors

ctor

Spice
public p ThreadPool.ctor(unsigned short workerThreadCount = 0s)

Create a new thread pool.

Parameters

Name Type Description
workerThreadCount unsigned short (default: 0s)

Methods

start

Spice
public p ThreadPool.start()

Start the thread pool.

stop

Spice
public p ThreadPool.stop()

Finish the running jobs and stop the thread pool.

join

Spice
public p ThreadPool.join()

Wait for all queued jobs to finish and stop the thread pool.

enqueue

Spice
public p ThreadPool.enqueue(const p()& job)

Enqueue a job to be run by the thread pool.

Parameters

Name Type Description
job const p()& The job routine to enqueue.

pause

Spice
public p ThreadPool.pause()

Pause the thread pool. The worker threads will finish their current job and then wait for the pool to be resumed.

resume

Spice
public p ThreadPool.resume()

Resume the thread pool.

isPaused

Spice
public f<bool> ThreadPool.isPaused()

Check if the thread pool is paused.

Returns: bool

getRunningJobCount

Spice
public f<unsigned short> ThreadPool.getRunningJobCount()

Retrieve the number of jobs that are currently running.

Returns: unsigned short

getQueuedJobCount

Spice
public f<unsigned short> ThreadPool.getQueuedJobCount()

Retrieve the number of jobs that are currently queued.

Returns: unsigned short

getWorkerThreadCount

Spice
public f<unsigned short> ThreadPool.getWorkerThreadCount()

Retrieve the number of worker threads.

Returns: unsigned short