11.2 Fibers and spawn
Start cooperative fibers with spawn, Fiber handles, join, detach, and cancel.
Fibers and spawn
Fiber<i32> worker = spawn DoWork(42);spawn is not “fire a thread.” It schedules a cooperative fiber with a typed handle.
Every spawn expression must type-check to Fiber<T> where T is the entry callable’s return type (Concurrency.Fiber in corelib_concurrency). You do not get T directly from spawn—use Join.
Normative feature: Fibers and spawn.
Handle and cancellation
Section titled “Handle and cancellation”The handle exposes OnCancelled as an event on the child fiber handle, not on the entry callable. Cancellation flows: Cancel → observe OnCancelled → Join / channel errors per the decisions record.
Semantic rules (cheat sheet)
Section titled “Semantic rules (cheat sheet)”| Rule | Consequence |
|---|---|
Stack references must not escape spawn | StackReferenceEscapesSpawn diagnostic |
| Detach waives shutdown join | Otherwise runtime joins non-detached children when main returns |
| Cross-fiber payload | Channels only — not mutex-as-mailbox |
Lowering
Section titled “Lowering”beskid_codegen emits fiber_spawn with environment captures rooted for GC. Runtime details: Fiber scheduler and stacks.