Skip to content

@threads

Import this module with import @threads.

Thread lifecycle management. Compiler-only feature; requires POSIX threads.

Function Signature Description
spawn (fn func()) -> Thread Spawn a new thread running fn, any func() value
spawn_arg (fn func(i64), arg i64) -> Thread Spawn a new thread running fn, any func(i64) value, passing arg as its i64 parameter
join (t Thread) Wait for a thread to finish
detach (t Thread) Release ownership; the thread runs independently. After detach the handle must not be joined or queried
is_alive (t Thread) -> bool True while the thread’s body has not returned. Not valid after detach or join
get_id () -> i64 Get the current thread’s ID
yield () Hint the scheduler to run another runnable thread
thread_count () -> i64 Number of live threads spawned through this module (excludes main and non-Grayscale threads)

A Thread that spawn did not return (a zero-value one), or one already passed to join or detach, is a runtime panic (P0170) in join, detach, and is_alive. join and detach clear the handle they are given, and a copy of that handle made earlier is rejected the same way.