c++ - 为什么 fetch_sub 不是释放操作?

标签 c++ multithreading c++11 concurrency memory-barriers

引自 C++ 并发实战 $ list 5.9

A fetch_sub operation with memory_order_acquire semantics doesn’t synchronize-with anything, even though it stores a value, because it isn’t a release operation. Likewise, a store can’t synchronize-with a fetch_or with memory_order_release semantics, because the read part of the fetch_or isn’t an acquire operation.

我很难理解上面的段落。如果 fetch_sub memory_order_acquire语义的操作不同步任何东西,为什么fetch_sub接口(interface)给我们留下一个内存顺序参数如下?

T fetch_sub( T arg, std::memory_order order = std::memory_order_seq_cst ) noexcept;

最佳答案

  1. “同步于”是单向的,不可交换的。 “A synchronizes with B”并不意味着“B synchronizes with A”(事实上,恰恰相反),这与人们对英语的期望不同。因此,memory_order_acquire RMW 操作无法与任何内容同步,但是 memory_order_release 存储与 memory_order_acquire RMW 操作同步,后者获取从中读取的值商店。同样,虽然 memory_order_release 存储不与 memory_order_release RMW 同步,但 memory_order_release RMW 可以与 memory_order_acquire 加载。
  2. memory_order_acq_rel

关于c++ - 为什么 fetch_sub 不是释放操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50500443/

相关文章:

c++ - 对于(自动指针 : vectorOfPointers) vs for(auto pointer : vectorOfPointers)

c++ - 就地归并排序

c++ - libcurl 无法获取 CURLINFO_EFFECTIVE_URL

java - 空指针异常和线程

.net - Windows 窗体线程和事件 - 传递事件的最有效方法?

ios - 显示 UIAlert 会使应用程序崩溃吗?

c++ - 如何在静态 constexpr 类成员中使用 make_tuple() ?

c++ - 做一个不能实例化的类?

c++ - 散列浮点值

c++ - 为什么cerr输出比cout快?