C++11 how to observe memory order in atomic::store and atomic::load

Update 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 .

Update 2 :
I check the disassembly code. However, I find no matter how I add code, the x.store always prior to y.store.
The problem should come from compiler (which doesn't reorder these code) instead of CPU (as far as I think).

Update :
After I read comments, it seems like I have to borrow a machine which's CPU is alpha, arm or 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.
However, no matter how much times I tried, I always get 1.
Is this because of my CPU is x86 architecture?

If not, how to fix this problem?
(BTW, I know CppMem. But it cannot use loop.)


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.

链接地址: http://www.djcxy.com/p/30774.html

上一篇: 如何调整SVG剪辑的大小

下一篇: C ++ 11如何观察atomic :: store和atomic :: load中的内存顺序