其他分享
首页 > 其他分享> > c – exchange或compare_and_exchange是否按修改顺序读取最后一个值?

c – exchange或compare_and_exchange是否按修改顺序读取最后一个值?

作者:互联网

我正在读安东尼威廉姆斯C++ Concurrency in Action.
在“理解轻松订购”部分,它有:

There are a few additional things you can tell the man in the cubicle, such as “write down this number, and tell me what was at the bottom of the list” (exchange) and “write down
this number if the number on the bottom of the list is that; otherwise tell me what I
should have guessed” (compare_exchange_strong), but that doesn’t affect the general
principle.

这是否意味着此类操作始终按修改顺序读取最后一个值(如果在约束之前没有其他内部线程发生)?即是否有一些缓存更新/等(即使在轻松的订购)?

解决方法:

是. C标准说(29.3 / 10):

Atomic read-modify-write operations shall always read the last value
(in the modification order) written before the write associated with
the read-modify-write operation.

交换和成功的compare_exchange_ {weak,strong}操作都是读 – 修改 – 写操作.

标签:memory-model,lock-free,c,c11,atomic
来源: https://codeday.me/bug/20190825/1723819.html