Contents

compareExchange(expected:desired:ordering:)

Perform an atomic compare and exchange operation on the current value, applying the specified memory ordering.

Declaration

func compareExchange(expected: consuming Value, desired: consuming Value, ordering: AtomicUpdateOrdering) -> (exchanged: Bool, original: Value)

Parameters

  • expected:

    The expected current value.

  • desired:

    The desired new value.

  • ordering:

    The memory ordering to apply on this operation.

Return Value

A tuple (exchanged, original), where exchanged is true if the exchange was successful, and original is the original value.

Discussion

This operation performs the following algorithm as a single atomic transaction:

atomic(self) { currentValue in
  let original = currentValue
  guard original == expected else { return (false, original) }
  currentValue = desired
  return (true, original)
}