// *** before: ***
final class CounterOld {
private let queue = DispatchQueue(label: "counter")
private var value = 0
func increment() { queue.sync { value += 1 } }
var current: Int { queue.sync { value } }
// nothing checked that every path went through the queue: one direct
// read added later, and the race was back
}
// *** in version 5.5: ***
actor Counter {
private var value = 0
func increment() { value += 1 } // inside the actor there is no await
var current: Int { value }
}
let counter = Counter()
await counter.increment()
print(await counter.current)
// the compiler itself forbids touching the state of an actor from the
// outside without await, so the protection cannot be forgotten.
// A method that touches nothing of the actor is marked nonisolated