Search code examples
catomicc11stdatomic

Atomic array in C11


Based on the reference, the _Atomic type specifier can not be used on arrays. Is there a standard way/workaround to be able to issue atomic_exchange calls for an object that is stored within the array?


Solution

  • The standard prohibits to apply the _Atomic specifier to an array as a whole, for example this

    typedef double at[5];
    _Atomic(at) atomic_array; // constraint violation
    

    But the array elements may well be atomic

    _Atomic(double) atomic_array[5]; // valid
    

    If you want the access to the array as a whole to be atomic, you'd have to encapsulate the array within a structure and then have the _Atomic around the structure.