std.thread
OS threads, Scope (joined at block end), Pool (parallel loops)
Thread
Section titled “Thread”pub Thread :: struct { token: i64,}Thread.ok
Section titled “Thread.ok”pub ok :: fn(self: *Self) -> boolfalse if spawn failed
Thread.join
Section titled “Thread.join”pub join :: fn(self: *Self) -> u32blocks, returns the exit code (8 bits on linux); don’t reuse after
Thread.detach
Section titled “Thread.detach”pub detach :: fn(self: *Self)no wait; it runs to completion on its own
Thread.is_running
Section titled “Thread.is_running”pub is_running :: fn(self: *Self) -> boolnon-blocking poll
pub spawn :: fn(entry: *void, arg: *void) -> Threadentry is fn(*void) -> u32 via core::fn_ptr; arg must outlive the thread
current_id
Section titled “current_id”pub current_id :: fn() -> u32pub sleep :: fn(ms: u32)at least ms ms
yield_now
Section titled “yield_now”pub yield_now :: fn()scheduler hint, cooperative back-off
cpu_count
Section titled “cpu_count”pub cpu_count :: fn() -> u32logical cpus; a sane default thread count
ThreadGroup
Section titled “ThreadGroup”pub ThreadGroup :: struct { tokens: [64]i64, count: usize,}–– ThreadGroup –– up to MAX_GROUP threads; join_all blocks until all are done
tokens: MAX_GROUP slots
ThreadGroup.spawn
Section titled “ThreadGroup.spawn”pub spawn :: fn(self: *Self, entry: *void, arg: *void)silently ignored past MAX_GROUP
ThreadGroup.join_all
Section titled “ThreadGroup.join_all”pub join_all :: fn(self: *Self)blocks until all are done, then resets for reuse
pub group :: fn() -> ThreadGrouppub Scope :: struct { arena: Arena, tasks: ?*ScopeTask, spawned: usize,}–– Scope –– zone kind; joined on every exit from the block, so captured locals stay valid
tasks: the spawned closures, the newest first
Scope.make
Section titled “Scope.make”pub make :: fn() -> Scopereserves nothing until the first spawn
Scope.spawn
Section titled “Scope.spawn”pub spawn :: fn(self: *Self, f: fn())runs f inline if no thread starts; captures are shared, checked by #spawns
Scope.join
Section titled “Scope.join”pub join :: fn(self: *Self)waits for all spawned so far; zone end does it anyway, spawning after is fine
Scope.count
Section titled “Scope.count”pub count :: fn(self: *Self) -> usizeScope.alloc_bytes
Section titled “Scope.alloc_bytes”pub alloc_bytes :: fn(self: *Self, n: usize) -> []u8zone protocol: closure memory
Scope.deinit
Section titled “Scope.deinit”pub deinit :: fn(self: *Self)zone end: join all, then free
pub Pool :: struct { arena: Arena, mu: Mutex, wake: CondVar, finished: CondVar, workers: []i64, started: bool, stopping: bool, job: fn, round: u64, next: usize, end: usize, chunk: usize, working: usize,}–– Pool –– zone kind for parallel loops; cores-1 workers plus the caller, started at the first job
wake: a job is posted, or the pool stopsfinished: the last worker is done with the jobworkers: their threads, started at the first jobround: jobs posted so farnext: the next index to takeworking: workers not yet done with the job
Pool.make
Section titled “Pool.make”pub make :: fn() -> Poolno thread starts until the first job
Pool.range
Section titled “Pool.range”pub range :: fn(self: *Self, start: usize, end: usize, f: fn(usize))f(i) for i in start..end across the pool; returns when every call is done
Pool.each
Section titled “Pool.each”pub each :: fn($T: type, self: *Self, items: borrow []T, f: fn(*T))f(&items[i]) across the pool; each call writes only its own item
Pool.threads
Section titled “Pool.threads”pub threads :: fn(self: *Self) -> usizeworkers + the caller
Pool.alloc_bytes
Section titled “Pool.alloc_bytes”pub alloc_bytes :: fn(self: *Self, n: usize) -> []u8zone protocol: closure memory
Pool.deinit
Section titled “Pool.deinit”pub deinit :: fn(self: *Self)zone end: stop the workers, then free
parallel
Section titled “parallel”pub parallel :: fn(start: usize, end: usize, f: fn(usize))one loop on a throwaway pool (a thread start per core); reuse a Pool for many loops