Concurrency Concepts
Awaited Values
Multiple async calls can be awaited and then combined with ordinary arithmetic.
Await two calls
awaited_values.swift
Replay: real traced execution (multi-file project)
func score(_ value: Int) async -> Int {
return value + 1
}
let base = 5
let first = await score(base)
let second = await score(base + 2)
let total = first + second
let message = "total=\(total)"
print(message)
func score(_ value: Int) async -> Int {
return value + 1
}
let base = 2
let first = await score(base)
let second = await score(base + 2)
let total = first + second
let message = "total=\(total)"
print(message)
func score(_ value: Int) async -> Int {
return value + 1
}
let base = 8
let first = await score(base)
let second = await score(base + 2)
let total = first + second
let message = "total=\(total)"
print(message)
base ← 5
5let base→ 5 = 5 //@base=2, 86let first = await score(base5)7let second = await score(base + 2)func score(_ value: Int) async -> Int
pass 1 of 21func score(_ value5: Int) async -> Int {2 return value5 + 13}first ← 6
5let base = 5 //@base=2, 86let first→ 6 = await score(base5)7let second = await score(base5 + 2)8let total = first + secondfunc score(_ value: Int) async -> Int
pass 2 of 21func score(_ value7: Int) async -> Int {2 return value7 + 13}second ← 8, total ← 14, message ← total=14
6let first = await score(base)7let second→ 8 = await score(base5 + 2)8let total→ 14 = first6 + second89let message→ total=14 = "total=\(total14)"1011print(messagetotal=14)outputtotal=14
base ← 2
5let base→ 2 = 26let first = await score(base2)7let second = await score(base + 2)func score(_ value: Int) async -> Int
pass 1 of 21func score(_ value2: Int) async -> Int {2 return value2 + 13}first ← 3
5let base = 26let first→ 3 = await score(base2)7let second = await score(base2 + 2)8let total = first + secondfunc score(_ value: Int) async -> Int
pass 2 of 21func score(_ value4: Int) async -> Int {2 return value4 + 13}second ← 5, total ← 8, message ← total=8
6let first = await score(base)7let second→ 5 = await score(base2 + 2)8let total→ 8 = first3 + second59let message→ total=8 = "total=\(total8)"1011print(messagetotal=8)outputtotal=8
base ← 8
5let base→ 8 = 86let first = await score(base8)7let second = await score(base + 2)func score(_ value: Int) async -> Int
pass 1 of 21func score(_ value8: Int) async -> Int {2 return value8 + 13}first ← 9
5let base = 86let first→ 9 = await score(base8)7let second = await score(base8 + 2)8let total = first + secondfunc score(_ value: Int) async -> Int
pass 2 of 21func score(_ value10: Int) async -> Int {2 return value10 + 13}second ← 11, total ← 20, message ← total=20
6let first = await score(base)7let second→ 11 = await score(base8 + 2)8let total→ 20 = first9 + second119let message→ total=20 = "total=\(total20)"1011print(messagetotal=20)outputtotal=20
awaited value
Awaiting each async call stores normal values that later statements can reuse.