Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

atomic_init — C23 atomic generic function

`atomic_init(obj, value)` from `<stdatomic.h>` initializes the pointed-to atomic object with `value`, including any extra state required by the implementation, and returns no value. If the storage has no declared type, the call gives it the effective atomic type `A`.

Version: C23

Syntax

void atomic_init(volatile A *obj, C value);

atomic_init(obj, value) from <stdatomic.h> initializes the pointed-to atomic object with value, including any extra state required by the implementation, and returns no value. If the storage has no declared type, the call gives it the effective atomic type A.

Use it to put an automatic-duration or allocated atomic object into a valid state before publishing its address to other threads. The example initializes an atomic_int to 42 and then genuinely reads it through atomic_load.

atomic_init is not a synchronizing operation: any concurrent access during initialization, even an atomic access, creates a data race. A non-atomic store to any byte of an atomic object, including through memcpy or memset, makes its representation indeterminate. Calling it from a signal handler has undefined behavior unless the signal results from abort or raise; default initialization gives static and thread-duration non-atomic_flag objects a valid state.

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 value;
    atomic_init(&value, 42);
    printf("%d\n", atomic_load(&value));
    return 0;
}

Result

42

Use it to put an automatic-duration or allocated atomic object into a valid state before publishing its address to other threads. The example initializes an `atomic_int` to `42` and then genuinely reads it through `atomic_load`.

Sources