What is the recommended way to ensure atomic execution
Generated on 8/8/2024
1 search
The recommended way to ensure atomic execution in Swift is by using the new low-level primitives introduced in Swift 6. Specifically, the synchronization module introduces atomics and mutexes:
-
Atomics: These are generic over any type that provides an efficient, lock-free implementation on the platform. Atomic values should always be stored in
let
properties for safe concurrent access. All operations on atomics are explicit with memory ordering arguments, similar to the C and C++ memory model. -
Mutexes: Like atomics, a mutex should be stored in a
let
property and can be safely accessed concurrently. All accesses to the storage protected by the mutex are via the closure passed to thewithLock
method, which ensures mutually exclusive access.
For more details, you can refer to the session What’s new in Swift (28:51).
A Swift Tour: Explore Swift’s features and design
Learn the essential features and design philosophy of the Swift programming language. We’ll explore how to model data, handle errors, use protocols, write concurrent code, and more while building up a Swift package that has a library, an HTTP server, and a command line client. Whether you’re just beginning your Swift journey or have been with us from the start, this talk will help you get the most out of the language.
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.
Consume noncopyable types in Swift
Get started with noncopyable types in Swift. Discover what copying means in Swift, when you might want to use a noncopyable type, and how value ownership lets you state your intentions clearly.
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.