catomicc11

Do we have atomic uint32 type in C?


sig_atomic_t is a typedef of int. But I am curious do we have an atomic type which is matched to uint32_t ?


Solution

  • C11 defines following typedefs to atomic types in <stdatomic.h>:

    atomic_bool
    atomic_char
    atomic_schar
    atomic_uchar
    atomic_short
    atomic_ushort
    atomic_int
    atomic_uint
    atomic_long
    atomic_ulong
    atomic_llong
    atomic_ullong
    atomic_char16_t
    atomic_char32_t
    atomic_wchar_t
    atomic_int_least8_t
    atomic_uint_least8_t
    atomic_int_least16_t
    atomic_uint_least16_t
    atomic_int_least32_t
    atomic_uint_least32_t
    atomic_int_least64_t
    atomic_uint_least64_t
    atomic_int_fast8_t
    atomic_uint_fast8_t
    atomic_int_fast16_t
    atomic_uint_fast16_t
    atomic_int_fast32_t
    atomic_uint_fast32_t
    atomic_int_fast64_t
    atomic_uint_fast64_t
    atomic_intptr_t
    atomic_uintptr_t
    atomic_size_t
    atomic_ptrdiff_t
    atomic_intmax_t
    atomic_uintmax_t
    

    There is no atomic_uint32_t, so your options are:

    • You can use _Atomic(uint32_t) directly.
    • You can use one of existing alternative types (atomic_uint_least32_t, atomic_uint_fast32_t or even atomic_char32_t) if this fits your purpose (probably it doesn't).
    • You can assume atomic_uint is 32-bit and use it as a replacement. This should be actually one of the most portable ways as most OS (*BSDs, Linux, Windows) assume int is a 32-bit type.