[英]C++11 how to observe memory order in atomic::store and atomic::load
Update 3 : 更新3 :
After understanding what "memory order" is, I know the problem is totally not related to compiler. 在了解了“内存顺序”之后,我知道问题与编译器完全无关。
And yes, because my CPU architecture is Intel x86, no matter what code I write, the memory order effect will never happen . 是的,因为我的CPU架构是Intel x86, 无论我编写什么代码,内存顺序效果都不会发生 。
Update 2 : 更新2 :
I check the disassembly code. 我检查了反汇编代码。 However, I find no matter how I add code, the x.store always prior to y.store. 但是,我发现无论我如何添加代码,x.store总是在y.store之前。
The problem should come from compiler (which doesn't reorder these code) instead of CPU (as far as I think). 问题应该来自编译器(它没有重新排序这些代码)而不是CPU(据我所知)。
Update : 更新 :
After I read comments, it seems like I have to borrow a machine which's CPU is alpha, arm or ppc. 在我阅读评论后,似乎我必须借用一台CPU为alpha,arm或ppc的机器。
Does anyone know where I can use this kind of machine even this is not for free? 有谁知道我在哪里可以使用这种机器,即使这不是免费的?
Origin: 起源:
I am testing the code below. 我正在测试下面的代码。
atomic<int> x(0);
atomic<int> y(0);
void thr1()
{
x.store(1,memory_order_relaxed);
y.store(1,memory_order_relaxed);
}
void thr2()
{
while(!y.load(memory_order_relaxed))
;
cout<<x.load(memory_order_relaxed)<<endl; //may 0 or 1
}
I know the output may be 0. 我知道输出可能是0。
However, no matter how much times I tried, I always get 1. 然而,无论我多少次尝试,我总是得到1。
Is this because of my CPU is x86 architecture? 这是因为我的CPU是x86架构吗?
If not, how to fix this problem? 如果没有,如何解决这个问题?
(BTW, I know CppMem. But it cannot use loop.) (顺便说一下,我知道CppMem。但它不能使用循环。)
What you are experiencing is not a "problem". 你所经历的不是“问题”。 At least, not as far as the standard is concerned. 至少,不是标准所涉及的。
When ordering is relaxed, this only means that ordering is no longer guaranteed. 放宽订购时,这仅表示不再保证订购。 This does not mean that implementations must put them into different orders. 这并不意味着实现必须将它们放入不同的顺序。
A different compiler may show it; 不同的编译器可能会显示它; then again, it may not. 然后,它可能不会。 Hell, just changing the optimization might cause it to happen. 地狱,只是改变优化可能会导致它发生。 Then again, maybe not. 然后,也许不是。 There is ultimately nothing you can do to guarantee seeing the other order (outside of emulation of some sort or similar tools). 最终没有什么可以保证看到其他顺序(在某些类型或类似工具的仿真之外)。 Just because you state that something might be possible does not ensure that it will or must happen. 仅仅因为你陈述某些可能的东西并不能确保它会或者必须发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.