stdatomic(C11),关于_原子类型的三个问题 [英] stdatomic (C11), three questions about _Atomic types

查看:513
本文介绍了stdatomic(C11),关于_原子类型的三个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在cppreference上找到

I found on cppreference

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

用作类型说明符;这指定了一种新的原子类型

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

_Atomic type-name(2)(自C11起)

_Atomic type-name (2) (since C11)

用作类型限定符;这指定了type-name的原子版本.尽管与其他限定符不同,但type-name的原子版本可能具有不同的大小,对齐方式和对象表示形式.

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.

使用_Atomic(int)代替_Atomic int也是 确保它与int一样大小?

So does using _Atomic(int) instead of _Atomic int guarantee it to be the same size as int or not?

_Atomic中使用限定符 例如:

Using a qualifier inside _Atomic Ex:

_Atomic(volatile int)

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

Throws an error, but using it like this:

_Atomic(volatile _Atomic(int)*)

不;这是标准行为吗?

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

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?

推荐答案

第一个问题:

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 :

[约束条件]

3原子类型说明符中的类型名称不得引用:数组类型,函数类型,原子类型或限定类型.

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是合格的类型.违反了约束部分中的<应>应,因此编译器需要输出诊断消息.除此之外,这种构造的行为是不确定的.

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.

第三个问题:

C11 7.17.1.p5 :

5在以下简介中:

5 In the following synopses:

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

他们希望使用_Atomic类型.您传入了非原子变量,因此未定义行为.

They expect an _Atomic type. You pass in a non-atomic variable, therefore undefined behaviour.

这篇关于stdatomic(C11),关于_原子类型的三个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆