q-thread-pool API Documentation

q-thread-pool

Simple thread pools based on concurrent queues.

Example Usage

Use make-thread-pool to create a thread pool with 16 threads and a backlog of 32 and bind it to pool.

(setq pool (make-thread-pool 16 32))

Use enqueue-task to enqueue 64 tasks for parallel evaluation by pool. Note that the body forms of enqueue-task is a lexical closure. The threads of pool will work off the backlog concurrently. Once the backlog is full, e.g. when there are already 32 other tasks waiting, enqueue-task will block until there is room in the queue.

(let ((out *standard-output*))
  (dotimes (i 64)
    (enqueue-task pool
      (sleep 5)
      (write-string "x" out))))

When pool is no longer needed, its threads should be destroy using destroy-thread-pool in order to avoid leaking resources.

(destroy-thread-pool pool)

destroy-thread-pool (Function)

Syntax:

— Function: destroy-thread-pool thread-pool

Arguments and Values:

thread-pool—a thread pool.

Description:

destroy-thread-pool destroys all threads allocated by thread-pool.

enqueue-task (Macro)

Syntax:

— Macro: enqueue-task thread-pool &body forms

Arguments and Values:

thread-pool—a thread pool.

formsforms.

Description:

enqueue-task enqueues a lexical closure containing forms for evaluation by thread-pool. If the backlog of of thread-pool is full enqueue-task will block until another task is dequeued.

make-thread-pool (Function)

Syntax:

— Function: make-thread-pool size backlog

Arguments and Values:

size, backlog—positive integers.

Description:

make-thread-pool returns a thread pool. Size is the number of threads in the pool. Backlog is the number of tasks that will be queued before enqueue-task blocks.