10.3 · async/await, tasks, task groups, cancellation and task locals

Swift concurrency turns “run this somewhere” into a tree: every task has a parent, a lifetime and a way to be cancelled. The shape of your code is the shape of your concurrency.

Mental model. A child task cannot outlive its scope. That one rule gives you automatic cancellation, error propagation and no leaked work.

Async code and task trees

Suspension, <code>async let</code>, task groups and detached work.

  1. What does async/await actually do, and what is a suspension point? 10.3.1 · L1
    An async function can suspend: at each await it may give up its thread and be resumed later, possibly on a different one. That is the whole idea. Compared with…
  2. How do tasks form a tree: Task, task groups, async let and detached tasks, and what does a child inherit? 10.3.2 · L3
    A structured child task is created by async let or group.addTask. It belongs to its parent’s scope: the scope does not finish until all its children do…

Cancellation and context

Cooperative cancellation, priorities and task locals.

  1. How does cancellation work in Swift concurrency, and what are task priorities and task locals for? 10.3.3 · L3
    Cancellation is cooperative and advisory. Calling task.cancel() (or cancelling a parent, or leaving a group by throwing) only sets a flag on the task and its…

More in Concurrency