Introducing Swift Atomics

I’m delighted to announce Swift Atomics, a new open source package that enables direct use of low-level atomic operations in Swift code. The goal of this library is to enable intrepid systems programmers to start building synchronization constructs (such as concurrent data structures) directly in Swift.

swift.org/blog/swift-atomics/

Swift Atomics is a new open-source package enabling low-level atomic operations in Swift, allowing systems programmers to build concurrent data structures safely. As demonstrated by a thread-safe counter example using ManagedAtomic.

It bypasses Swift’s exclusivity rules for atomic access, following SE-0282’s C/C++-style memory model, and supports types like integers, Booleans, pointers, and strong references, with specialised constructs for lazy initialisation.

The package offers memory-safe ManagedAtomic and flexible UnsafeAtomic wrappers, ensuring lock-free operations but not always wait-free due to potential compare-and-exchange loops. Developers are urged to use it cautiously, isolating atomic code and leveraging Thread Sanitizer to catch issues, given its complexity and low-level nature.


Category:

Tag:

Year: