compareAndExchange(expected:desired:)

Atomically compares the value against expected and, if they are equal, replaces the value with desired.

NIOAtomic.swift:253

This declaration is deprecated: please use ManagedAtomic from https://github.com/apple/swift-atomics instead

func compareAndExchange(expected: T, desired: T) -> Bool

Parameters

expected

The value that this object must currently hold for the compare-and-swap to succeed.

desired

The new value that this object will hold if the compare succeeds.

Returns

True if the exchange occurred, or False if expected did not match the current value and so no exchange occurred.

This implementation conforms to C11’s atomic_compare_exchange_strong. This means that the compare-and-swap will always succeed if expected is equal to value. Additionally, it uses a sequentially consistent ordering. For more details on atomic memory models, check the documentation for C11’s stdatomic.h.