Skip to the content.

gcc atomic

其实内容并不多: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html

此外,clang 会有不同吗? clang 的文档在哪里,我猜测应该都是一样的吧

__atomic_compare_exchange_n

一直在使用这个函数,但是没搞懂这里的几个参数都是做什么的,这里问了一下 codex ,回答应该是非常正确的: 还有一些疑问这个到底是如何实现的,但是暂时先这样了:

  1. 是如何提供 success_memorder 和 failure_memorder 的,也许看看
  2. 为什么会有 weak 这种失败的情况
Built-in Function: bool __atomic_compare_exchange_n (type *ptr, type *expected, type desired, bool weak, int success_memorder, int failure_memorder)
This built-in function implements an atomic compare and exchange operation. This compares the contents of *ptr with the contents of *expected. If equal, the operation is a read-modify-write operation that writes desired into *ptr. If they are not equal, the operation is a read and the current contents of *ptr are written into *expected. weak is true for weak compare_exchange, which may fail spuriously, and false for the strong variation, which never fails spuriously. Many targets only offer the strong variation and ignore the parameter. When in doubt, use the strong variation.

If desired is written into *ptr then true is returned and memory is affected according to the memory order specified by success_memorder. There are no restrictions on what memory order can be used here.
  bool __atomic_compare_exchange_n(
      type *ptr,
      type *expected,
      type desired,
      bool weak,
      int success_memorder,
      int failure_memorder
  );

核心语义:

  1. 比较 *ptr 和 *expected
  2. 如果相等:
    • 把 desired 写入 *ptr
    • 返回 true
  3. 如果不相等:
    • 不写 desired
    • 把当前 *ptr 的值回写到 *expected
    • 返回 false

weak

典型理解:

do { expected = old; } while (!__atomic_compare_exchange_n(&x, &expected, new, true, …));

这里用 weak=true 很常见,因为失败了就继续重试。

success_memorder

这个表示:

常见值:

直觉上:

failure_memorder

这个表示:

因为失败时只是读,没有写,所以它不能带 release 语义。

所以 GCC 要求:

常见搭配:

  - success = __ATOMIC_ACQUIRE, failure = __ATOMIC_RELAXED
  - success = __ATOMIC_ACQ_REL, failure = __ATOMIC_ACQUIRE
  - success = __ATOMIC_SEQ_CST, failure = __ATOMIC_SEQ_CST

本站所有文章转发 CSDN 将按侵权追究法律责任,其它情况随意。