DevPrep

All topics › Swift Concurrency (async/await, actors)

What is an actor and which data race does it prevent?

Senior PRO

The essence

An actor is a reference type whose mutable state is isolated: it can be touched only from inside the actor, and from outside only via await. The compiler checks isolation statically, and at runtime the actor has a serial executor — a queue of jobs executed one at a time without blocking threads: a task waiting for the actor is suspended, not parked on a mutex. This is how an actor rules out data races — simultaneous memory access with a write. It is not a mutex wrapped in syntax: a mutex blocks a thread, an actor suspends a task.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store