简体   繁体   English

C ++ 11如何在atomic :: store和atomic :: load中观察内存顺序

[英]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.

相关问题 C ++ 11使用非原子变量的原子内存顺序 - C++11 Atomic memory order with non-atomic variables MOV x86指令是否实现了C ++ 11 memory_order_release原子存储? - Does the MOV x86 instruction implement a C++11 memory_order_release atomic store? C++11 原子:std::memory_order 代码是否可移植? - C++11 atomic: is std::memory_order code portable? 具有内存顺序的原子负载存储 - atomic load store with memory order C ++编译器如何支持C ++ 11原子,但不支持C ++ 11内存模型 - How can C++ compilers support C++11 atomic, but not support C++11 memory model c ++ 11原子排序:锁的扩展总命令memory_order_seq_cst - c++11 atomic ordering: extended total order memory_order_seq_cst for locks 标准C ++ 11是否保证memory_order_seq_cst阻止StoreLoad在原子周围重新排序非原子? - Does standard C++11 guarantee that memory_order_seq_cst prevents StoreLoad reordering of non-atomic around an atomic? 内存顺序放宽的原子加载和存储 - Atomic load and store with memory order relaxed 在C ++ 11 std :: atomic中,用于++, - 和+ =等运算符的memory_order是什么? - In C++11 std::atomic, what is the memory_order used for operators like ++, — and +=? 在C11 / C ++ 11中,可以在同一个内存中混合原子/非原子操作吗? - In C11/C++11, possible to mix atomic/non-atomic ops on the same memory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM