Skip to content

Concurrency API

Every concurrency function in one table. For when to use which, and how they fit together, read the Concurrency guide. Promise is imported from std/runtime but documented in the guide, so its rows link there.

FunctionDescriptionDocumented in
Task.spawn(move () => {...})Spawn a green taskstd/runtime
t.join()Wait for a task to finishstd/runtime
Promise(fn) / Promise<T>.run(fn)Run fn on a green task, result via awaitConcurrency: Promises
Promise<T>.blocking(fn)Run fn on an OS thread (CPU-bound / blocking FFI)Concurrency: Promise.blocking
p.await()Wait for a promise's resultConcurrency: Promises
Promise.all(v) / Promise.race(v)Collect all results / first to finishConcurrency: Promise.all, Promise.race
parallelMap(v, n, f)Divide a Vec across n OS threads, transform in place, reassemble (std/shard)std/shard
parallelMapWith(v, windows, states, f)The same cycle with per-worker state and a window queuestd/shard
parallelScanStr(s, n, overlap, f)Divide a string into read-only windows and scan on n threadsstd/shard
Channel.new(cap)Create bounded channelstd/sync
ch.send(val)Send value (blocks if full)std/sync
ch.recv()Receive value (blocks if empty)std/sync
ch.trySend(val)Non-blocking send, returns boolstd/sync
ch.tryRecv()Non-blocking receive, returns Option<T>std/sync
ch.close()Signal no more valuesstd/sync
ch.len()Current items in channelstd/sync
WaitGroup.new()Create a wait groupstd/sync
wg.add(n) / wg.done() / wg.wait()Track and await a fleet of tasksstd/sync
AtomicI64.new(v) / AtomicI32.new(v) / AtomicU64.new(v) / AtomicBool.new(v)Create atomicstd/sync
a.load()Atomic readstd/sync
a.store(v)Atomic writestd/sync
a.add(v) / a.sub(v)Atomic add/sub, wrapping (returns old)std/sync
a.cas(exp, des)Compare-and-swap (returns old)std/sync
a.swap(v)Atomic swap (returns old)std/sync
Once.new()Create a run-exactly-once guardstd/sync
o.run(fn)Run fn once; later callers block until it finishesstd/sync
o.isDone()True once the initializer has completedstd/sync
x.clone()Give another task/worker its own owner of a channel, wait group, atomic, or Oncestd/sync