Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

atomic_load — C23 atomic generic function

`atomic_load(object)` atomically returns the value of the pointed-to atomic object. The non-`_explicit` form is equivalent to `atomic_load_explicit(..., memory_order_seq_cst)`; the pointer’s `volatile` qualification preserves device-register semantics.

Version: C23

Syntax

C atomic_load(const volatile A *object);

atomic_load(object) atomically returns the value of the pointed-to atomic object. The non-_explicit form is equivalent to atomic_load_explicit(..., memory_order_seq_cst); the pointer’s volatile qualification preserves device-register semantics.

Use a load to obtain a coherent snapshot of a counter or flag without racing atomic writers. The example increments a counter from 41 to 42; both genuine reads—the sequentially consistent and relaxed forms—then return 42.

atomic_load_explicit must not use memory_order_release or memory_order_acq_rel; relaxed still guarantees atomicity and the object’s modification order but does not publish other data. The object must be validly initialized and the pointer valid; the operation need not be lock-free.

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 counter = 41;
    atomic_fetch_add(&counter, 1);
    int sequential = atomic_load(&counter);
    int relaxed = atomic_load_explicit(&counter, memory_order_relaxed);
    printf("%d %d\n", sequential, relaxed);
    return 0;
}

Result

42 42

Use a load to obtain a coherent snapshot of a counter or flag without racing atomic writers. The example increments a counter from `41` to `42`; both genuine reads—the sequentially consistent and relaxed forms—then return `42`.

Sources