关于c: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 中使用限定符
例如:

1
_Atomic(volatile int)

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

1
_Atomic(volatile _Atomic(int)*)

没有;这是标准行为吗?

最后一个问题

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

  • “我注意到原子函数(例如:atomic_store、atomic_load、atomic_compare_exchange_weak)在传递的类型不是 _Atomic 类型的情况下工作”是什么意思?根据 C11 草案,例如 atomic_load() 需要一个参数,它是指向原子类型的指针。
  • “抛出错误” – 它抛出什么错误?!
  • @Ilja Everil?我正在传递任何类型的指针,仍然可以正常工作
  • 在 C 中,如果调用 UB,很多时候事情似乎可以正常工作,直到它们不起作用。
  • 请不要一次问几个问题,真的。这使得有用的搜索变得更加困难。现在,您甚至可以将其拆分,给他们有用的问题标题,然后用您现在从 Antti 的答案中知道的内容自己回答。


第一个问题:

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

第三题:

C11 7.17.1.p5:

5 In the following synopses:

  • An A refers to one of the atomic types.


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


来源:https://www.codenong.com/47980009/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9193.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?