Skip to content

SpecificationXI · Concurrency and asynchrony

11.1

Suspendible units and Await

A Suspendible unit can pause half-way while it waits for something; Await marks in the code every point where that pause may happen.

Markdown version

11.1.1 How it works

Subroutine Suspendible declares a subprogram that can suspend: stop running for a moment while it waits for another task, a channel or a host effect. The main algorithm is suspendible without being marked.

Every call to a suspendible unit must be preceded by Await. That way, reading the code, you see exactly where the program can pause: nowhere else.

Figure 11.1-1.The task runs up to Await, pauses while another one works and continues with the result. Outside the marked points it is never interrupted.

Two timelines. Task A works, pauses at Await while task B works, and continues when B hands it the result.

ProgramA suspendible call without Await

Subroutine Suspendable Doble(n As Integer) As Integer
  Return n * 2;
EndSubroutine

Algorithm SinAguardar
  Write Doble(2);
EndAlgorithm

Diagnostics

  • suspendibleCallWithoutAwait · error · 6:9 · A call to a suspendable subprogram must use await

The program finishes with an error.

Await can go in any position of an expression. What was computed before the wait is kept.

In short

  • Suspendible declares a unit that can pause.
  • Every pause point is marked with Await.
  • Between two Awaits, a task is not interrupted.