简体   繁体   English

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

[英]stdatomic (C11), three questions about _Atomic types

First question 第一个问题

I found on cppreference 我发现cppreference

  • _Atomic ( type-name ) (since C11) _Atomic ( type-name ) (自C11起)

    Use as a type specifier; 用作类型说明符; this designates a new atomic type 这指定了一种新的原子类型

  • _Atomic type-name (2) (since C11) _Atomic type-name (2)(自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. 尽管与其他限定符不同,type-name的原子版本可能具有不同的大小,对齐方式和对象表示形式,但它可以与const,volatile和strict混合使用。

So does using _Atomic(int) instead of _Atomic int guarantee it to be the same size as int or not? 那么使用_Atomic(int)而不是_Atomic int确保它与int大小相同吗?

Second question 第二个问题

Using a qualifier inside _Atomic Ex: _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. 我注意到原子功能(例如: atomic_storeatomic_loadatomic_compare_exchange_weak ),而不通过类型为工作_Atomic类型,我还是可以管理的竞争条件,没有问题。 Is this standard behaviour? 这是标准行为吗? Does it have downsides or lead to any error? 它有缺点或导致任何错误吗?

First question: 第一个问题:

C11 7.17.6p3 : 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 : 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. 3原子类型说明符中的类型名称不得引用数组类型,函数类型,原子类型或限定类型。

volatile int is a qualified type. volatile int是合格的类型。 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 : C11 7.17.1.p5

5 In the following synopses: 5在以下概要中:

  • An A refers to one of the atomic types. A是指原子类型之一。

They expect an _Atomic type. 他们期望使用_Atomic类型。 You pass in a non-atomic variable, therefore undefined behaviour. 您传入了非原子变量,因此未定义行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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