Concurrency Concepts
Isolated State
Concurrent programs keep mutable state behind a small boundary.
Update through one boundary
isolated_state.swift
struct CounterState {
var value = 0
mutating func add(_ amount: Int) {
value += amount
}
}
let amount =
var state = CounterState()
state.add(amount)
let message = "counter=\(state.value)"
print(message)
struct CounterState {
var value = 0
mutating func add(_ amount: Int) {
value += amount
}
}
let amount =
var state = CounterState()
state.add(amount)
let message = "counter=\(state.value)"
print(message)
struct CounterState {
var value = 0
mutating func add(_ amount: Int) {
value += amount
}
}
let amount =
var state = CounterState()
state.add(amount)
let message = "counter=\(state.value)"
print(message)
isolated state
This small type models the same idea actors enforce: callers update state through a method instead of changing storage from many places.