为什么 fetch_sub 不是释放操作?
Why isn't fetch_sub a release operation?
引自 C++ 并发实战 $清单 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
的接口给我们留下一个内存顺序参数如下?
T fetch_sub( T arg, std::memory_order order = std::memory_order_seq_cst ) noexcept;
- "synchronize with" 是单向的,不可交换。 "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
负载同步。
memory_order_acq_rel
.
引自 C++ 并发实战 $清单 5.9
A
fetch_sub
operation withmemory_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 afetch_or
withmemory_order_release
semantics, because the read part of thefetch_or
isn’t an acquire operation.
我很难理解上面的段落。如果 fetch_sub
使用memory_order_acquire
语义的操作不与任何东西同步,为什么fetch_sub
的接口给我们留下一个内存顺序参数如下?
T fetch_sub( T arg, std::memory_order order = std::memory_order_seq_cst ) noexcept;
- "synchronize with" 是单向的,不可交换。 "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
负载同步。 memory_order_acq_rel
.