std.isolate

Independent multi-core workers and their channels.

Importuse std.isolate

Types

struct Isolate#

std.isolate worker handle.

_handle: int

Functions

extern fn spawn<T, R>(entry: string, args: [T]) -> Isolate<R>#

Starts an isolate entry point with copied arguments.

  • entry: Name of the entry-point function to run in the new isolate.
  • args: Arguments copied across the isolate boundary.
fn double(value: int) -> int { value * 2 }

fn main() {
  let worker = std.isolate.spawn("double", [21])
  println(std.isolate.join(worker)) // 42
}
extern fn join<R>(isolate: Isolate<R>) -> R#

Waits for an isolate and returns its result.

  • isolate: Isolate to wait for.
extern fn channel(capacity: int) -> int#

Creates an isolate channel and returns its identifier.

  • capacity: Buffered slots the isolate channel holds.
fn producer(channel: int) -> int {
  std.isolate.send(channel, "moon")
  std.isolate.close(channel)
  1
}

fn main() {
  let channel = std.isolate.channel(1)
  let worker = std.isolate.spawn("producer", [channel])
  println(std.isolate.recv<string>(channel).unwrap())
  println(std.isolate.recv<string>(channel).is_none())
  println(std.isolate.join(worker))
}
extern fn send<T>(channel: int, value: T) -> bool#

Sends a value through the channel.

  • channel: Identifier returned by channel.
  • value: Value copied to the receiving isolate.
extern fn recv<T>(channel: int) -> Optional<T>#

Receives the next channel value, or none after closure.

extern fn close(channel: int)#

Closes the resource.