sigildocs

(sigil threads)

(sigil threads) - Share-Nothing Threading

Safe parallelism through isolated worker threads with message passing. Each worker runs in its own VM with independent heap, symbol table, and module registry. Values are serialized when crossing thread boundaries.

Basic Usage

(import (sigil threads))

(define w (thread-spawn (lambda () (* 6 7))))
(thread-join w)  ; => 42

Parallel Map

(parallel-map (lambda (x) (* x x)) '(1 2 3 4))  ; => (1 4 9 16)

Exports

thread-spawnprocedure

Spawn a worker thread running a zero-argument closure.

The closure and its captured values are serialized to the worker's VM. Returns a thread-worker object. The closure must only capture serializable values (no ports, continuations, or foreign objects).

Examples:

(thread-spawn (lambda () (+ 1 2)))  ; => #<thread-worker>
thread-joinprocedure

Wait for a worker to finish and return its result.

Blocks until the worker completes. If the worker encountered an error, raises an error in the calling thread.

Examples:

(thread-join (thread-spawn (lambda () 42)))  ; => 42
thread-sendprocedure

Send a value to a worker's inbox.

The value is serialized before sending. Only serializable values can be sent (numbers, strings, symbols, lists, vectors, dicts).

Examples:

(thread-send worker '(compute 42))

Receive a value from a worker's outbox.

Blocks until a message is available. Returns eof-object when the worker has finished and no more messages are available.

Examples:

(thread-receive worker)  ; blocks until message available
parallel-mapprocedure

Apply a procedure to each element of a list in parallel.

Partitions the list into chunks, spawns worker threads to process each chunk, and collects results. The number of workers defaults to 4 but can be configured with the workers: keyword.

Examples:

(parallel-map (lambda (x) (* x x)) '(1 2 3 4))  ; => (1 4 9 16)
(parallel-map heavy-compute data workers: 8)

Check if value is a thread worker

Re-exported from (sigil threads native)

thread-alive?procedure

Check if a worker thread is still running

Re-exported from (sigil threads native)