10.10 · Sendable rules, locks versus actors, and the escape hatches

Swift 6 makes data races compile errors. The practical questions are what counts as Sendable, which protection mechanism to pick, and what you are promising when you reach for an escape hatch.

Mental model. A type may cross isolation only if it is safe to share: immutable, a value, or internally synchronised. Everything else needs an owner.

Sendable and protection

The Sendable rules, and a mutex, atomic, actor or queue for shared state.

  1. When is a type Sendable, and what does the compiler check when you declare it? 10.10.1 · L3
    Sendable means a value can be safely used from several concurrency domains at once. The rules are mostly mechanical. Value types (structs and enums) are…
  2. How do you choose between an actor, a Mutex, an unfair lock, an atomic and a serial queue to protect shared state? 10.10.2 · L5
    All four counters in the check reach exactly 1000 after a thousand concurrent increments, so correctness is not what separates them; the choice is about what…

Escape hatches

@unchecked Sendable, nonisolated(unsafe), and what the compiler still checks.

  1. What are @unchecked Sendable and nonisolated(unsafe) promising, and what does the compiler still check when you use them? 10.10.3 · L5
    @unchecked Sendable says “trust me: this type is safe to share, I protect its state myself”. The compiler stops checking the stored properties, so the whole…

More in Concurrency