Skip to content

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.task

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.

import std.sys
import std.task
// Size a worker pool to logical cores
workers := sys.cpu_count()
for i in 0..workers {
task.spawn(|_| { /* work */ })
}
// Cache sizing from available memory
cache_mb := sys.avail_mem() / (1024 * 1024) / 4 // quarter of avail mem
import std.cell
import std.task
// Shared counter across tasks
func 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)
}
import std.task
import 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_recv
import std.chan
import std.task
// Simple producer/consumer
func 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)
}

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.

import std.task
import 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 .err
q := task.try_join(h) // .some when done, .none while running

Join handles dispatch method calls: h.join(), h.try_join().

import std.task
h := task.spawn(|_| { 40 + 2 })
r: int = h.join() // 42
import std.task
// Fan-out / fan-in
func 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)
}

3 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.

Function Signature Description
std.task.join join<T>(handle: task.join) -> T Wait for result (panics → .err)
std.task.spawn spawn<T>(f: func(T) -> T) -> task.join Spawn a green thread
std.task.try_join try_join<T>(handle: task.join) -> Option<Result<T, str>> Poll result (repeatable)
  • std.chan — Channels: send, receive and non-blocking probes between green threads.

Full index: API reference · Standard library