Module

std::thread

source

Threads: spawn runs a closure on a new OS thread, and Thread!::join waits for it and returns what it returned.

The closure moves what it captured into the new thread. There is no detach: every spawned thread's result must reach a join, so a linear value it produces can never be stranded with nobody left to destroy it.

spawn is safe to call. Holding a value does not by itself grant permission to act on it, so moving an environment into another thread is inert by itself: a linear environment transfers exclusive ownership, and the checker rejects any further use of what was captured; one that is Copy with no raw pointer in it is plain data, with nothing to race on; and one that is or holds a raw pointer grants no access by existing, since every dereference of it, in either thread, needs its own unsafe.

Shared mutable state should cross as std::cap::Exclusive! or Shared! rather than a bare pointer: both are linear, so moving or splitting one into the closure is the same ownership transfer as any linear value. A closure cannot mention a brand its block opened (let exists 't. ...), so a capability over one is sent by a function generic over the brand, where it is a parameter, which calls spawn: forall 't. fn work(tag: Exclusive!['t], bytes: Slice[u8, 't]).

Types

SpawnPayloadThe closure spawn hands the new thread, boxed on the heap for trampoline to take.
ResultBoxpresent gives the box bytes even when Ret has none ((), a capability), so it can be stored and loaded through a pointer.
Thread!A thread spawn started, whose closure returns a Ret; join waits for it.

Functions

trampolineRuns on the new thread, instantiated once per (Env, Ret).
spawnSpawns an OS thread running f(); join returns f's result, typed as Ret, which may be a zero-size type such as ().

Function declarations

fn trampolinesource

forall Env, Ret.
unsafe fn trampoline(raw: *u8) -> *u8 { ... }

Runs on the new thread, instantiated once per (Env, Ret). Takes ownership of the heap payload spawn built, frees the box, calls the closure exactly once, and boxes the result for join to unbox.

Caller: raw is the SpawnPayload[Env, Ret] box spawn built for this same (Env, Ret), owned by nothing else. It is read, freed and its closure called; any other pointer is read and freed as one.

fn spawnsource

forall Env, Ret.
fn spawn(f: closure[Env]() -> Ret) -> Result[Thread![Ret], (closure[Env]() -> Ret, Error)] { ... }

Spawns an OS thread running f(); join returns f's result, typed as Ret, which may be a zero-size type such as (). The module's overview explains why this is safe to call. When no thread can be started, f comes back unrun with the error, so what it captured is still the caller's.