Thread API Reference

OS-level threading with thread pools and scoped threads

Import

U std/thread

Constants

ConstantValueDescription
THREAD_NEW0Thread created but not started
THREAD_RUNNING1Thread is running
THREAD_FINISHED2Thread has finished
THREAD_DETACHED3Thread is detached

Structs

JoinHandle<T>

Handle for a spawned thread. Call join() to wait for completion.

MethodSignatureDescription
joinF join(&self) -> T?Wait and get result
is_finishedF is_finished(&self) -> i64Check if done
idF id(&self) -> i64Get thread ID

Thread

Represents a running thread with id and name.

MethodSignatureDescription
newF new(id: i64, handle: i64) -> ThreadCreate thread handle
with_nameF with_name(id: i64, handle: i64, name: str) -> ThreadCreate with name
idF id(&self) -> i64Get thread ID
nameF name(&self) -> strGet thread name
unparkF unpark(&self) -> i64Unpark the thread

ThreadBuilder

Configurable thread spawner (set name, stack size).

MethodSignatureDescription
newF new() -> ThreadBuilderCreate builder
nameF name(&self, name: str) -> ThreadBuilderSet thread name
stack_sizeF stack_size(&self, size: i64) -> ThreadBuilderSet stack size
spawnF spawn(&self, fn_ptr: i64, arg: i64) -> JoinHandle<i64>Spawn with options

ThreadLocal<T>

Thread-local storage.

MethodSignatureDescription
newF new(init_fn: i64) -> ThreadLocal<T>Create with initializer
getF get(&self) -> &TGet thread-local value
setF set(&self, value: T) -> i64Set thread-local value

ThreadPool

Basic thread pool for submitting tasks.

MethodSignatureDescription
newF new(num_threads: i64) -> ThreadPoolCreate pool
submitF submit(&self, fn_ptr: i64, arg: i64) -> i64Submit task
shutdownF shutdown(&self) -> i64Shutdown pool

Scope

Scoped threads that auto-join on scope exit.

MethodSignatureDescription
newF new() -> ScopeCreate scope
spawnF spawn(&self, fn_ptr: i64, arg: i64) -> i64Spawn scoped thread
join_allF join_all(&self) -> i64Wait for all threads

Key Functions

FunctionSignatureDescription
spawnF spawn(fn_ptr: i64, arg: i64) -> JoinHandle<i64>Spawn a new thread
spawn_closureF spawn_closure(closure_ptr: i64, env_ptr: i64) -> JoinHandle<i64>Spawn with closure
currentF current() -> ThreadGet current thread
yield_nowF yield_now() -> i64Yield to other threads
sleep_msF sleep_ms(ms: i64) -> i64Sleep milliseconds
sleepF sleep(secs: i64) -> i64Sleep seconds
parkF park() -> i64Park current thread
park_timeoutF park_timeout(ms: i64) -> i64Park with timeout
builderF builder() -> ThreadBuilderCreate thread builder
create_poolF create_pool(num_threads: i64) -> ThreadPoolCreate thread pool
scopeF scope(scope_fn: i64) -> i64Run with scoped threads
available_parallelismF available_parallelism() -> i64Get CPU core count

Usage

U std/thread

F worker(arg: i64) -> i64 { arg * 2 }

F main() -> i64 {
    handle := spawn(worker, 21)
    result := handle.join()  # Some(42)
    0
}