Skip to content

← labtoy2026-09-06

Actors

Swift actor isolation, drawn as rooms with doors. One task inside at a time, the rest wait outside, and every await opens the door again.

Task 1 is inside Counter, then awaits Logger. Awaiting means leaving the room, so Task 2 walks in. When Task 1 comes back, Counter's state is not what it left behind.

waiting to startreturnedCounteremptyLoggerempty12
tick 0
  1. Press play.
actor Counter {
    var value = 0

    func incrementAndLog() async {
        value += 1
        await logger.log("\(value)")  // suspension point
        // Someone else may have run here.
        // Do not trust the value you wrote.
        value += 1
    }
}

An actor serialises access to its state. A suspension point inside an actor method is not a lock held across the await: the actor is free while you wait, and someone else may run. Read your state again after every await, or do not await in the middle.