thread

OS-level thread spawning and thread identification.

Import

import thread

Functions

thread.spawn(fn: () -> any) -> handle

Spawn a new OS thread and return a handle. Call .join() on the handle to wait for completion.

thread.id() -> int

Current thread ID.

thread.cpu_count() -> int

Number of logical CPU cores available.

thread.sleep(secs: float)

Sleep the current thread for the given number of seconds.

Examples

import thread

println("cores: {thread.cpu_count()}")

let t = thread.spawn(fn() {
    thread.sleep(0.1)
    println("from thread {thread.id()}")
    return 42
})

println("main thread: {thread.id()}")
let result = t.join()
println("thread returned: {result}")