std.task - ZZ
Spawn and join green threads. Tasks run in microseconds; the executor (one parked OS thread per CPU, work-stealing Chase–Lev deques) multiplexes thousands of them. Snapshot isolation: a spawn gets deep copies of captures.
import std.taskPurpose
Section titled “Purpose”std.sys reports platform identity and resources. Use cpu_count for task.spawn fan-out, total_mem/avail_mem for cache sizing and diagnostics headers.
Production patterns
Section titled “Production patterns”import std.sysimport std.task
// Size a worker pool to logical coresworkers := sys.cpu_count()for i in 0..workers { task.spawn(|_| { /* work */ })}
// Cache sizing from available memorycache_mb := sys.avail_mem() / (1024 * 1024) / 4 // quarter of avail memProduction patterns
Section titled “Production patterns”import std.cellimport std.task
// Shared counter across tasksfunc main() -> Result<int, str> { counter := cell.new(0) for i in 0..8 { task.spawn(|_| { cur := cell.get(counter).unwrap_or(0) match cell.set(counter, cur + 1) { .ok(_) => (), .err(e) => println(e), } }) } // read final value println(cell.get(counter).unwrap_or(0)) cell.drop(counter) return .ok(0)}Fan-out / fan-in recipe
Section titled “Fan-out / fan-in recipe”import std.taskimport std.chan
jobs := chan()out := chan()
for i in 0..4 { task.spawn(|_| { while true { match chan.try_recv(jobs) { .some(j) => chan.send(out, j * 2), .none => break, } } })}for i in 0..100 { chan.send(jobs, i) }// Drain `out` with recv/try_recvProduction patterns
Section titled “Production patterns”import std.chanimport std.task
// Simple producer/consumerfunc main() -> Result<int, str> { c := chan() task.spawn(|_| -> Result<int, str> { chan.send(c, 42) return .ok(0) }) v: int = chan.recv(c) println(v) // 42 return .ok(0)}Purpose
Section titled “Purpose”std.task.spawn builds a task; join waits for its result; try_join polls. A spawn gets deep copies of captures plus every function it transitively calls — after spawn, sides share nothing but channels and join handles, so no data races by construction. Blocking calls suspend the task, never the thread. Every for/while has a safepoint so CPU loops stay fair.
Quickstart
Section titled “Quickstart”import std.taskimport std.chan
c := chan()for i in 0..8 { task.spawn(|_| { chan.send(c, i * i) })}for i in 0..8 { v: int = chan.recv(c) println(v)}
h := task.spawn(|_| { 21 * 2 })r: Result<int, str> = task.join(h) // panics inside surface as .errq := task.try_join(h) // .some when done, .none while runningMethod syntax
Section titled “Method syntax”Join handles dispatch method calls: h.join(), h.try_join().
import std.task
h := task.spawn(|_| { 40 + 2 })r: int = h.join() // 42Production patterns
Section titled “Production patterns”import std.task
// Fan-out / fan-infunc main() -> Result<int, str> { h1 := task.spawn(|_| -> int { 10 }) h2 := task.spawn(|_| -> int { 20 }) a: int = task.join(h1) b: int = task.join(h2) println(a + b) // 30 return .ok(0)}API reference
Section titled “API reference”3 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.
See also
Section titled “See also”std.chan— Channels: send, receive and non-blocking probes between green threads.
Full index: API reference · Standard library