具有memory_order_relaxed的存储区可能永远不会到达其他线程吗? [英] Is it possible that a store with memory_order_relaxed never reaches other threads?

查看:82
本文介绍了具有memory_order_relaxed的存储区可能永远不会到达其他线程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个线程 A ,它使用 x.store( atomic_int x = 0; 1,std :: memory_order_relaxed); 。如果没有任何其他同步方法,则使用 x.load(std :: memory_order_relaxed); 需要多长时间才能看到其他线程?在给定标准提供的C / C ++内存模型的当前定义的情况下,写入 x 的值是否可能完全保持线程局部性?

Suppose I have a thread A that writes to an atomic_int x = 0;, using x.store(1, std::memory_order_relaxed);. Without any other synchronization methods, how long would it take before other threads can see this, using x.load(std::memory_order_relaxed);? Is it possible that the value written to x stays entirely thread-local given the current definition of the C/C++ memory model that the standard gives?

我手头的实际情况是线程 B 经常读取 atomic_bool 来检查它是否具有退出;另一个线程有时将 true 写入此bool,然后在线程B上调用join()。显然,我不介意在线程B甚至看到atomic_bool被设置之前就调用join()。 ,也不必介意线程B在调用join()之前已经看到了更改并退出了执行。但是我想知道:在两端都使用 memory_order_relaxed ,是否可以调用join()并永久阻止块,因为更改永远不会传播到线程B?

The practical case that I have at hand is where a thread B reads an atomic_bool frequently to check if it has to quit; Another thread, at some point, writes true to this bool and then calls join() on thread B. Clearly I do not mind to call join() before thread B can even see that the atomic_bool was set, nor do I mind when thread B already saw the change and exited execution before I call join(). But I am wondering: using memory_order_relaxed on both sides, is it possible to call join() and block "forever" because the change is never propagated to thread B?

我联系了马克·巴蒂(Mark Ba​​tty)(进行数学验证并随后确定C ++内存模型要求的大脑)。最初是关于其他事情的(后来发现这是cppmem和他的论文中的一个已知错误;所以幸运的是,我并没有完全愚弄自己,也借此机会也问了他;他的答案是:

I contacted Mark Batty (the brain behind mathematically verifying and subsequently fixing the C++ memory model requirements). Originally about something else (which turned out to be a known bug in cppmem and his thesis; so fortunately I didn't make a complete fool of myself, and took the opportunity to ask him about this too; his answer was:


问:从理论上讲,这样的商店[memory_order_relaxed如果没有(任何后续)释放操作)永远不会到达另一个线程吗?

标记:从理论上讲,是的,但我认为这没有被观察到。

问:换句话说,宽松的商店毫无意义吗?除非您将它们与某个释放操作(以及在另一个线程上获得的
)结合使用,否则假设您希望另一个线程让
看到它?

标记:几乎所有


推荐答案

该标准在29.3.12中是这样说的:

This is what the standard says in 29.3.12:


实施应该使atomi c在合理的时间内存储了对原子负载可见的存储。

Implementations should make atomic stores visible to atomic loads within a reasonable amount of time.

不能保证存储将在另一个线程中可见,没有保证的时间,并且与内存顺序没有正式关系。

There is no guarantee a store will become visible in another thread, there is no guaranteed timing and there is no formal relationship with memory order.

当然,在每个常规体系结构上,商店 都是可见的,但是在罕见的不支持缓存一致性的平台,对于负载来说,它可能永远不会变得可见。

在这种情况下,您将不得不接触原子 read-modify-write 操作以获取修改顺序中的最新值。

Of course, on each regular architecture a store will become visible, but on rare platforms that do not support cache coherency, it may never become visible to a load.
In that case, you would have to reach for an atomic read-modify-write operation to get the latest value in the modification order.

这篇关于具有memory_order_relaxed的存储区可能永远不会到达其他线程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆