<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Synchronization/Atomic/weakCompareExchange(expected:desired:successOrdering:failureOrdering:)-9kx2t",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Synchronization"
    ],
    "preciseIdentifier" : "s:15Synchronization6AtomicVA2A19_Atomic32BitStorageV0B14RepresentationRtzrlE19weakCompareExchange8expected7desired15successOrdering07failureM0Sb9exchanged_x8originaltxn_xnAA0b6UpdateM0VAA0b4LoadM0VtF"
  },
  "title" : "weakCompareExchange(expected:desired:successOrdering:failureOrdering:)"
}
-->

# weakCompareExchange(expected:desired:successOrdering:failureOrdering:)

Perform an atomic weak compare and exchange operation on the current
value, applying the specified success/failure memory orderings. This
compare-exchange variant is allowed to spuriously fail; it is designed to
be called in a loop until it indicates a successful exchange has happened.

```
func weakCompareExchange(expected: consuming Value, desired: consuming Value, successOrdering: AtomicUpdateOrdering, failureOrdering: AtomicLoadOrdering) -> (exchanged: Bool, original: Value)
```

## Parameters

`expected`

The expected current value.

`desired`

The desired new value.

`successOrdering`

The memory ordering to apply if this
operation performs the exchange.

`failureOrdering`

The memory ordering to apply on this
operation does not perform the exchange.

## 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)
}
```

The `ordering` argument specifies the memory ordering to use when the
operation manages to update the current value, while `failureOrdering`
will be used when the operation leaves the value intact.

> Note: The weakCompareExchange form may sometimes return false even when
> the original and expected values are equal. (Such failures may happen
> when some transient condition prevents the underlying operation from
> succeeding – such as an incoming interrupt during a
> load-link/store-conditional instruction sequence.) This variant is
> designed to be called in a loop that only exits when the exchange is
> successful; in such loops, using weakCompareExchange may lead to a
> performance improvement by eliminating a nested loop in the regular,
> “strong”, compareExchange variants.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)