atomic_compare_exchange_weak (C11)

Purpose

Performs an atomic compare and exchange operation.

Prototypes

_Bool atomic_compare_exchange_weak(volatile A *object, C *expected, C desired);

Parameters

A
One of the atomic types.
C
The corresponding non-atomic type of A.
object
A pointer that points to the atomic object to test and modify.
expected
A pointer that points to the value expected to be found in the atomic object.
desired
The value to be stored in the atomic object if the comparison result is true.

Usage

This function atomically compares the value pointed to by object for equality with the value pointed to by expected.

If the comparison result is true, this function replaces the value pointed to by object with desired.

If the comparison result is false, this function updates the value pointed to by expected with the value pointed to by object.

A weak compare-and-exchange operation might fail spuriously. That is, even when the contents of memory pointed to by expected and object are equal, it might return zero and store back to the place pointed to by expected the same memory contents that were originally there.

Return value

Returns the result of the comparison.