BT

InfoQ Homepage News Swift Atomics Enables First-Class Atomic Operations in Swift

Swift Atomics Enables First-Class Atomic Operations in Swift

Bookmarks

Swift Atomics aims to allow system programmers to write synchronization constructs directly in Swift.

Swift Atomics provides developers aiming to build sophisticated, high-level, multi-threaded constructs, such as lock-free data structure, the opportunity to do so directly in Swift. Previously, to use atomic operations in Swift, developers had to usually resort to external libraries implemented in a different language.

While striving to make atomic operations easier to use for Swift developers, Swift does not remove the inherent risk with any program using low-level synchronization primitives and is not meant to be a library for every Swift developer out there.

The Atomics package provides carefully considered APIs for atomic operations that follows established design principles for Swift APIs. However, the underlying operations work on a very low level of abstraction. Atomics – even more than other low-level concurrency constructs – are notoriously difficult to use correctly.

So, most Swift developers will continue to use Grand Central Dispatch and queues for their asynchronous code.

The following is an example of how you can use Swift Atomics to write a program that increments a counter shared by multiple threads:

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

ManagedAtomic is the easiest way to use atomic values, since it supports the usual reference counted behaviour. For cases where this is excessive overhead, UnsafeAtomic is available, which can be used with any piece of memory for which you can get a pointer. Both ManagedAtomic and UnsafeAtomic provide the same set of operations, including load, store, exchange, compareExchange, and weakCompareExchange.

Swift Atomics does not only support Int, as shown in the example above, but a plethora of other types including the whole integers and unsigned integer family, boolean, pointers, references, enums, etc.

Code using atomic operations is not bound to Swift exclusivity memory access, which prevents variables from begin accessed under a different name while they are being modified in a different portion of the program. When using Swift 5, a violation of the exclusive access rule will usually cause a crash, but atomic operations are handled differently. This is made possible by a relaxation of the Swift memory model meant to make it possible to interoperate with low-level primitives imported from C/C++. So, the new rule that governs Swift Exclusivity Access states that:

Two accesses to the same variable aren't allowed to overlap unless both accesses are reads or both accesses are atomic.

In the above sentence, atomic access is defined as a call to a number of functions belonging to the C atomic operation library.

Swift Atomics extends Swift suitability for concurrent programming, although work on a real concurrency model, which will introduce some form of support for async/await and actors, is still in progress for Swift 6. In the next months, the Swift team will work to introduce tagged atomics and support for atomic floating point operations.

 

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.
午夜电影_免费高清视频|高清电视|高清电影_青苹果影院