Structured tasks and cancellation
Structured tasks and cancellation
A report can request two independent scores before combining them. Structured concurrency links child tasks to their enclosing scope, helping organize their lifetime. Async let starts a child operation while allowing another independent child to be started before their results are awaited.
What you’ll learn
- Start independent structured children and await both results.
- Separate deterministic aggregation from child completion order.
Structured tasks and cancellation in the example
Start children
A report can request two independent scores before combining them.
Start childrenAwait required values
The parent must handle child results and errors according to the construct’s rules.
Await required valuesCombine deterministic result
Two child operations are created and the result expression waits for both values.
Combine deterministic result
How structured tasks and cancellation works
The parent must handle child results and errors according to the construct’s rules. Cancellation is cooperative: work needs to observe cancellation where appropriate. Starting concurrent work does not guarantee a speedup or a particular order of internal side effects.
Work through structured tasks and cancellation
Save this complete program as main.swift and run it with the Swift toolchain. Use a toolchain supporting the demonstrated async top-level code.
Swift · complete program
func score(_ value: Int) async -> Int { value }
async let first = score(80)
async let second = score(90)
let total = await first + second
print("Total:", total)Expected output
Total: 170Follow the execution
Two child operations are created and the result expression waits for both values. Only the final total is printed, so the transcript does not depend on which child was scheduled first. The toy operations return immediately and do not demonstrate a measurable performance benefit.
Try this program in the Swift compiler. Paste the complete source above.
Practice: Structured tasks and cancellation
Your task: Can you infer the first child finished first because it appears first in source?
Swift · complete program
func score(_ value: Int) async -> Int { value }
async let first = score(80)
async let second = score(90)
let total = await first + second
print("Total:", total)Worked answer
No. Creation order is not a completion-order guarantee for independent work. Await the required values or explicitly coordinate operations when their order matters.
Do not infer completion order from creation
Task groups handle dynamically sized child collections. Unstructured Task and detached tasks have different lifetime and isolation considerations; avoid treating them as interchangeable with structured children. Check cancellation and cleanup for real long-running work.
Check your decision
Do not infer completion order from creation
No. Creation order is not a completion-order guarantee for independent work. Await the required values or explicitly coordinate operations when their order matters. Task groups handle dynamically sized child collections.
Further reading: official Swift language guide.
Reaching the recap and completing the quizzes are separate milestones.