Free Download Video

Apple brings low-level atomic operations to Swift language

Apple has published Swift Atomics, an open source package that enables systems programmers to build synchronization constructs, such as concurrent data structures, directly in the Swift language.

The Atomics library, introduced October 1 and available on GitHub, enables direct use of low-level atomic operations in Swift. Atomic operations are enabled on a variety of Swift types including integers and pointer values. APIs for atomics operations are provided that follow design principles for Swift APIs.

Apple did offer words of caution: Underlying operations work on a very low level of abstraction. Even more than low-level concurrency constructs, atomics are notoriously difficult to use correctly, said Karoy Larentey, an Apple engineer on the Swift standard library team.

An example of atomic operations was published at

import Atomics
import Dispatch

let counter = ManagedAtomic<Int>(0)

DispatchQueue.concurrentPerform(iterations: 10) { _ in
  for _ in 0 ..< 1_000_000 {
    counter.wrappingIncrement(by: 1, ordering: .relaxed)
counter.load(ordering: .relaxed) // ⟹ 10_000_000

These operations do not follow normal exclusivity rules for Swift variables, the author noted. Atomic operations can be performed from multiple, concurrent threads of execution as long as the value is only accessed through atomic operations.

Atomics was enabled via a Swift evolution proposal that adopted a C/C++ memory style for Swift and described how regular Swift code interoperates with atomic operations. Most APIs in the new package were derived from previous versions of the evolution proposal.  

Plans for Atomics call for adding more types and improving the current test suite. Tagged atomics is eyed, providing a tool to solve problems with concurrent data structures. An Atomics forum has been set up for discussing the technology. Also, support for atomic floating point operations has been requested.

Related posts

GitHub adds CodeQL scanning for security bugs


How IT Leaders Can Apply Facial Recognition Tech Responsibly


Tips to Help IT Leaders Navigate Organizational Change