Can an atomic release be “overwritten”?

最后都变了- 提交于 2020-08-05 07:31:31

问题


Say I have atomic<int> i; Thread A performs an atomic store/exchange with memory_order_release. Next, Thread B performs an atomic store with memory_order_release. Thread C performs an atomic fetch_add(0, memory_order_acquire);

Does Thread C acquire dependencies from thread A and B or only thread B?


回答1:


Only B (I'm going to assume that by "next" you mean the modification order of the atomic is A -> B -> C so that by [atomics.order]p11 C's RMW must read the value B wrote). See the note in [intro.races]p6:

Except in the specified cases, reading a later value does not necessarily ensure visibility as described below. Such a requirement would sometimes interfere with efficient implementation.

The read part of the fetch_add is an acquire operation that took its value from the store-release, so the store release synchronizes with the RMW by [atomics.order]p2:

An atomic operation A that performs a release operation on an atomic object M synchronizes with an atomic operation B that performs an acquire operation on M and takes its value from any side effect in the release sequence headed by A.

However, the store/release performed by thread B is not an RMW operation and therefore is not part of the release sequence headed by thread A's store (see [intro.races]p5). Therefore, thread A's store doesn't synchronize with the fetch_add.




回答2:


edit: see answer of T.C.

this leaves only this part of my answer:

i'd strongly recommend to use atomics with their default memory order (memory_order_seq_cst) unless you have a really good reason (measure performance) to do otherwise.



来源:https://stackoverflow.com/questions/50573323/can-an-atomic-release-be-overwritten

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!