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.
- What does async/await actually do, and what is a suspension point?
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… - How do tasks form a tree: Task, task groups, async let and detached tasks, and what does a child inherit?
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.
- How does cancellation work in Swift concurrency, and what are task priorities and task locals for?
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
- 10.1 · Queues, QoS, barriers, groups, semaphores and sources
- 10.2 · Operation, OperationQueue, dependencies and cancellation
- 10.4 · Actors, reentrancy, isolated, nonisolated, global actors and @MainActor
- 10.5 · Region-based isolation, sending, and migrating to Swift 6
- 10.6 · Continuations and bridging GCD, Combine and callback APIs
- 10.7 · Deadlocks, main-actor hops, Thread Sanitizer and Instruments
- 10.8 · Task groups, async let, cancellation and task locals in practice
- 10.9 · Isolation rules, isolated parameters, global actors, main-actor hops and default isolation
- 10.10 · Sendable rules, locks versus actors, and the escape hatches
- 10.11 · Groups, semaphores, parallel loops, operation dependencies, QoS and timers
- 10.12 · AsyncStream buffering, continuation misuse and notifications as async sequences