Isolated var
Generated on 10/3/2024
1 search
In the context of migrating to Swift 6, the term "isolated var" refers to the concept of ensuring that variables are safely accessed in a concurrent environment to prevent data races. In Swift 6, you can address data races by using several strategies:
-
Immutable Variables: Convert a
var
to alet
to make it immutable, which prevents data races when accessed from multiple threads. -
Global Actors: Tie a global variable to a global actor, such as the main actor, to ensure that all access to the variable is isolated to a specific actor.
-
Non-Isolated Unsafe: Use the
nonisolated unsafe
keyword as a last resort when you have an external mechanism ensuring the safety of the variable, such as a dispatch queue. This approach places the responsibility on the developer to ensure thread safety. -
Assume Isolated: Use
assume isolated
to inform the compiler that a piece of code is already running on a specific actor, such as the main actor, without starting a new task.
These strategies are part of the broader effort in Swift 6 to enhance data-race safety and concurrency management. For more detailed information, you can refer to the session Migrate your app to Swift 6 (13:05) which discusses shared mutable state in global variables.
Migrate your app to Swift 6
Experience Swift 6 migration in action as we update an existing sample app. Learn how to migrate incrementally, module by module, and how the compiler helps you identify code that’s at risk of data races. Discover different techniques for ensuring clear isolation boundaries and eliminating concurrent access to shared mutable state.
Explore Swift performance
Discover how Swift balances abstraction and performance. Learn what elements of performance to consider and how the Swift optimizer affects them. Explore the different features of Swift and how they’re implemented to further understand the tradeoffs available that can impact performance.
What’s new in Swift
Join us for an update on Swift. We’ll briefly go through a history of Swift over the past decade, and show you how the community has grown through workgroups, expanded the package ecosystem, and increased platform support. We’ll introduce you to a new language mode that achieves data-race safety by default, and a language subset that lets you run Swift on highly constrained systems. We’ll also explore some language updates including noncopyable types, typed throws, and improved C++ interoperability.