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.
- When is a type Sendable, and what does the compiler check when you declare it?
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… - How do you choose between an actor, a Mutex, an unfair lock, an atomic and a serial queue to protect shared state?
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.
- What are @unchecked Sendable and nonisolated(unsafe) promising, and what does the compiler still check when you use them?
@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
- 10.1 · Queues, QoS, barriers, groups, semaphores and sources
- 10.2 · Operation, OperationQueue, dependencies and cancellation
- 10.3 · async/await, tasks, task groups, cancellation and task locals
- 10.4 · Actors, reentrancy, isolated, nonisolated, global actors and @MainActor
- 10.5 · Region-based isolation, sending, and migrating to Swift 6
- 10.6 · Continuations and bridging GCD, Combine and callback APIs
- 10.7 · Deadlocks, main-actor hops, Thread Sanitizer and Instruments
- 10.8 · Task groups, async let, cancellation and task locals in practice
- 10.9 · Isolation rules, isolated parameters, global actors, main-actor hops and default isolation
- 10.11 · Groups, semaphores, parallel loops, operation dependencies, QoS and timers
- 10.12 · AsyncStream buffering, continuation misuse and notifications as async sequences