Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

atomic_store — C23 atomic generic function

`atomic_store(object, desired)` atomically replaces the pointed-to object’s value with `desired` and returns no value. The non-`_explicit` form uses `memory_order_seq_cst`; this is a write, not a read-modify-write operation.

Version: C23

Syntax

void atomic_store(volatile A *object, C desired);

atomic_store(object, desired) atomically replaces the pointed-to object’s value with desired and returns no value. The non-_explicit form uses memory_order_seq_cst; this is a write, not a read-modify-write operation.

Use a release store to publish completed state that a consumer reads with an acquire load from the same atomic object. The example stores 17 and then 23; a genuine acquire load observes the final value 23.

atomic_store_explicit must not receive memory_order_consume, memory_order_acquire, or memory_order_acq_rel; relaxed stores atomically but does not publish preceding ordinary writes. The object must be initialized and the pointer valid; the implementation may use a lock.

This generic function name may be a macro or an external identifier; suppressing the macro to call an actual function, or defining an external identifier with that name, has undefined behavior.

Examples

#include <stdatomic.h>
#include <stdio.h>

int main(void) {
    atomic_int state = 0;
    atomic_store(&state, 17);
    atomic_store_explicit(&state, 23, memory_order_release);
    printf("%d\n", atomic_load_explicit(&state, memory_order_acquire));
    return 0;
}

Result

23

Use a release store to publish completed state that a consumer reads with an acquire load from the same atomic object. The example stores `17` and then `23`; a genuine acquire load observes the final value `23`.

Sources