Io module

spawn

procedureextensionexperimental2 overloadsSource

Starts proc as a new fiber on the current scheduler and returns its handle. It first runs when the calling fiber blocks or yields, and not at all if the scheduler ends first. The new fiber starts with a copy of the caller's context and temporary storage of its own. Fibers share one OS thread, so they can use shared data without locks as long as they do not block in the middle of updating it.

Example↗
Io :: #import "Extensions/Io";

main :: () {
    Io.io_run(() {
        results := Io.make_channel(int, 1);
        defer Io.free_channel(results);

        Io.spawn((out: *Io.Channel(int)) { Io.send(out, 42); }, results);

        value, error := Io.recv(results); // waits for the spawned fiber
        assert(error == .NONE && value == 42);
    });
}
spawn :: (proc: $P) -> Fiber_Handle

Parameters

proc: $P

Returns

spawn :: (proc: $P, data: $T) -> Fiber_Handle

The spawn for a procedure that takes one argument: data is copied for the new fiber, so a pointer in it must stay valid until the fiber is done with it.

Parameters

proc: $P
data: $T

Returns

See also