stdatomic (C11), three questions about _Atomic types

喜夏-厌秋 提交于 2020-06-28 06:37:29

问题


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?


回答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.



来源:https://stackoverflow.com/questions/47980009/stdatomic-c11-three-questions-about-atomic-types

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!