Swift & Concurrency (50 Qs)•Part 3: Runtime Internals, Swift 6 Safety & Systems Programming (25 - 35)
Concurrency: Actors vs Global Actors (`@MainActor`) and Actor Reentrancy hazards.
senior LevelswiftRuntime Internals
In Simple Words:
- Actors: Reference types that protect internal mutable state by synchronizing access through a private serial mailbox queue.
@MainActor: A global actor that guarantees code executes strictly on the primary UI thread (crucial for view updates).- Actor Reentrancy Hazard: When an actor method calls
await, it releases its execution thread while waiting. Other tasks can execute on the actor in the meantime, potentially mutating internal state before the original method resumes! Always re-verify assumptions after everyawaitpoint.