stdatomic (C11),关于_Atomic类型的三个问题
stdatomic (C11), three questions about _Atomic types
第一题
我在 cppreference 上找到了
_Atomic ( type-name )
(自 C11 起)
用作类型说明符;这指定了一个新的原子类型
_Atomic type-name
(2)(自 C11 起)
用作类型限定符;这指定类型名称的原子版本。在这个角色中,它可能与 const、volatile 和 restrict) 混合使用,尽管与其他限定符不同,type-name 的原子版本可能具有不同的大小、对齐方式和对象表示。
使用 _Atomic(int)
而不是 _Atomic int
也是如此
是否保证它与 int
大小相同?
第二个问题
在 _Atomic
中使用限定词
例如:
_Atomic(volatile int)
抛出错误,但像这样使用它:
_Atomic(volatile _Atomic(int)*)
没有;这是标准行为吗?
最后一个问题
我注意到原子函数(例如:atomic_store
、atomic_load
、atomic_compare_exchange_weak
)在没有传递类型为 _Atomic
类型的情况下工作,我仍然可以管理竞争条件没问题。
这是标准行为吗?它有缺点或导致任何错误吗?
第一个问题:
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.
第二个问题:
[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,因此编译器需要输出诊断消息。除此之外,这种构造的行为是未定义的。
第三题:
5 In the following synopses:
- An
A
refers to one of the atomic types.
他们期望 _Atomic
类型。您传入了一个非原子变量,因此是未定义的行为。
第一题
我在 cppreference 上找到了
_Atomic ( type-name )
(自 C11 起)用作类型说明符;这指定了一个新的原子类型
_Atomic type-name
(2)(自 C11 起)用作类型限定符;这指定类型名称的原子版本。在这个角色中,它可能与 const、volatile 和 restrict) 混合使用,尽管与其他限定符不同,type-name 的原子版本可能具有不同的大小、对齐方式和对象表示。
使用 _Atomic(int)
而不是 _Atomic int
也是如此
是否保证它与 int
大小相同?
第二个问题
在 _Atomic
中使用限定词
例如:
_Atomic(volatile int)
抛出错误,但像这样使用它:
_Atomic(volatile _Atomic(int)*)
没有;这是标准行为吗?
最后一个问题
我注意到原子函数(例如:atomic_store
、atomic_load
、atomic_compare_exchange_weak
)在没有传递类型为 _Atomic
类型的情况下工作,我仍然可以管理竞争条件没问题。
这是标准行为吗?它有缺点或导致任何错误吗?
第一个问题:
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.
第二个问题:
[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,因此编译器需要输出诊断消息。除此之外,这种构造的行为是未定义的。
第三题:
5 In the following synopses:
- An
A
refers to one of the atomic types.
他们期望 _Atomic
类型。您传入了一个非原子变量,因此是未定义的行为。