LEVIATHAN v962456e · 962456eee1

leviathan-lang.com / docs / learn

Concurrency

Honest promises, await, workers for real parallelism, and where channels and task groups fit.

Promises and await

Concurrency is honest: a function that returns a Promise<T> returns an actual promise. There is no implicit wrapping, no async keyword, and no function coloring. await is the one privileged operation, and it parks the current task until the promise settles.

Returning and awaiting a promise

Promise<int> slowSquare(int n) {
    => Promise(n * n);        // an actual Promise, returned honestly
}

int squared = await slowSquare(6);   // 36: parks until it settles
console.writeln(squared);
36

Workers

For real parallelism, std::spawn starts a worker. The handle is a promise, so await is the join. Workers are fully isolated: everything crossing the boundary crosses by copy.

Spawning a worker and joining it

int heavyCompute(Array<int> data) {
    int total = 0;
    for (int x in data) { total = total + x * x; }
    return total;
}

Array<int> data = [1, 2, 3, 4];
Worker<int> w = std::spawn(() => heavyCompute(data));
console.writeln("doing other work");   // ... do other work meanwhile ...
int result = await w;                  // join the worker, get its result
console.writeln(result);
doing other work
30

Channels and task groups

Workers talk through a Channel<T>, and structured concurrency comes from TaskGroup over the same using rule you already know: no task outlives its scope. The reference entries on tasks and cancellation cover tasks, cancellation, and awaitTimeout; the library pages for Channel and TaskGroup list their members.

See also