stdatomic (C11),关于_Atomic类型的三个问题

stdatomic (C11), three questions about _Atomic types

第一题

我在 cppreference 上找到了

使用 _Atomic(int) 而不是 _Atomic int 也是如此 是否保证它与 int 大小相同?

第二个问题

_Atomic 中使用限定词 例如:

_Atomic(volatile int)

抛出错误,但像这样使用它:

_Atomic(volatile _Atomic(int)*)

没有;这是标准行为吗?

最后一个问题

我注意到原子函数(例如:atomic_storeatomic_loadatomic_compare_exchange_weak)在没有传递类型为 _Atomic 类型的情况下工作,我仍然可以管理竞争条件没问题。 这是标准行为吗?它有缺点或导致任何错误吗?

第一个问题:

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.

第二个问题:

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 是限定类型。违反约束部分中的 shall,因此编译器需要输出诊断消息。除此之外,这种构造的行为是未定义的。

第三题:

C11 7.17.1.p5:

5 In the following synopses:

  • An A refers to one of the atomic types.

他们期望 _Atomic 类型。您传入了一个非原子变量,因此是未定义的行为。