简体   繁体   中英

stdatomic (C11), three questions about _Atomic types

First question

I found on cppreference

  • _Atomic ( type-name ) (since C11)

    Use as a type specifier; this designates a new atomic type

  • _Atomic type-name (2) (since C11)

    Use as a type qualifier; this designates the atomic version of type-name. In this role, it may be mixed with const, volatile, and restrict), although unlike other qualifiers, the atomic version of type-name may have a different size, alignment, and object representation.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM