---
title: "weakCompareExchange(expected:desired:ordering:)"
framework: synchronization
role: symbol
role_heading: Instance Method
path: "synchronization/atomic/weakcompareexchange(expected:desired:ordering:)-728eh"
---

# weakCompareExchange(expected:desired:ordering:)

Perform an atomic weak compare and exchange operation on the current value, applying the memory ordering. 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.

## Declaration

```swift
func weakCompareExchange(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

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

## Discussion

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) } 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.
