Goroutines in Chapel

Here’s the explanation and translated code in Chapel following the structure and details provided in the example.


A task is a lightweight thread of execution.

Suppose we have a function call f(s). Here’s how we’d call that in the usual way, running it synchronously.

proc f(from: string) {
  for i in 0..<3 {
    writeln(from, " : ", i);
  }
}

proc main() {
  f("direct");

  // To invoke this function in a separate task, we use 'begin'
  begin f("task");

  // We can also start a task for an anonymous function call
  begin {
    writeln("going");
  }
  
  // Our two function calls are running asynchronously.
  // Wait for them to finish
  // (for a more robust approach, use a `sync` or `atomic` variable).
  sleep(1);
  writeln("done");
}

main();

When we run this program, we see the output of the blocking call first, then the output of the two tasks. The tasks’ output may be interleaved because tasks are being run concurrently by the Chapel runtime.

$ chpl goroutines.chpl
$ ./goroutines
direct : 0
direct : 1
direct : 2
task : 0
going
task : 1
task : 2
done

Next, we’ll look at a complement to tasks in concurrent Chapel programs: channels.