Apple proposes actor model for Swift concurrency
- 20 March, 2021 07:45
Apple has published a proposal to add an actor model to the Swift programming language. The goal is to provide a mechanism that prevents data races in the Swift concurrency model.
A Swift evolution proposal in the active review stage, posted on GitHub, notes that the actor model would provide the ability to use shared mutable state while providing static detection of data races and other common concurrency bugs.
An actor is a reference type (introduced by the keyword a
ctor :) that protects access to its mutable state. In response to messages, an actor can make local decisions, send more messages, create more actors, and decide how to respond to the next message received.
Actors can modify their own private state but only affect each other indirectly through messaging, thus eliminating the need for lock-based synchronisation.
A concurrency roadmap for the Swift language published last fall included a proposal for actors and actor isolation. A structured concurrency proposal for Swift introduces concurrent tasks and provides data race safety for functions and closures.
While this model is suitable for some design patterns, including parallel maps and concurrent callback patterns, it is limited to working with state captured by closures. Although Swift includes classes that provide a mechanism for declaring mutable state that is shared across a program, classes are difficult to use within concurrent programs.
The actor proposal is set to be reviewed until March 29. The review process was opened on March 15.