Alex Sim
Alex Sim

Reputation: 423

stdatomic (C11), three questions about _Atomic types

First question

I found on cppreference

So does using _Atomic(int) instead of _Atomic int guarantee it to be the same size as int or not?

Second question

Using a qualifier inside _Atomic Ex:

_Atomic(volatile int)

Throws an error, but using it like this:

_Atomic(volatile _Atomic(int)*)

Does not; is this standard behaviour?

Last question

I noticed atomic functions (ex: atomic_store, atomic_load, atomic_compare_exchange_weak) work without the passed types being _Atomic types, and I can still manage race conditions with no problem. Is this standard behaviour? Does it have downsides or lead to any error?

Upvotes: 2

Views: 1682

Answers (1)

First question:

C11 7.17.6p3:

NOTE The representation of atomic integer types need not have the same size as their corresponding regular types. They should have the same size whenever possible, as it eases effort required to port existing code.

Second question:

C11 6.7.2.4p3:

[Constraints]

3 The type name in an atomic type specifier shall not refer to an array type, a function type, an atomic type, or a qualified type.

volatile int is a qualified type. A shall in a constraints section is violated, therefore the compiler needs to output a diagnostics message. Beyond that, the behaviour of such a construct is undefined.

Third question:

C11 7.17.1.p5:

5 In the following synopses:

  • An A refers to one of the atomic types.

They expect an _Atomic type. You pass in a non-atomic variable, therefore undefined behaviour.

Upvotes: 5

Related Questions