Function

GLibatomic_pointer_compare_and_exchange

since: 2.4

Declaration

gboolean
g_atomic_pointer_compare_and_exchange (
  void* atomic,
  gpointer oldval,
  gpointer newval
)

Description

Compares atomic to oldval and, if equal, sets it to newval. If atomic was not equal to oldval then no change occurs.

This compare and exchange is done atomically.

Think of this operation as an atomic version of { if (*atomic == oldval) { *atomic = newval; return TRUE; } else return FALSE; }.

This call acts as a full compiler and hardware memory barrier.

While atomic has a volatile qualifier, this is a historical artifact and the pointer passed to it should not be volatile.

Available since: 2.4

Parameters

atomic

Type: void*

A pointer to a #gpointer-sized value.

The data is owned by the caller of the function.
oldval

Type: gpointer

The value to compare with.

The argument can be NULL.
The data is owned by the caller of the function.
newval

Type: gpointer

The value to conditionally replace with.

The argument can be NULL.
The data is owned by the caller of the function.

Return value

Type: gboolean

TRUE if the exchange took place.