admin管理员组文章数量:1415476
I read memory-order things in .In the 'Explanation' section, an example is given,
// Thread 1:
r1 = y.load(std::memory_order_relaxed); // A
x.store(r1, std::memory_order_relaxed); // B
// Thread 2:
r2 = x.load(std::memory_order_relaxed); // C
y.store(42, std::memory_order_relaxed); // D
and it says
...In particular, this may occur if D is completed before C in thread 2, either due to compiler reordering or at runtime
I'm wondering that as C is sequenced-before D, how could compiler reorder C and D?
From , the definition of sequenced-before guarantees that both value computation and side effect of an expression A will be completed before another expression B if A is sequenced-before B.
I read memory-order things in https://en.cppreference/w/cpp/atomic/memory_order.In the 'Explanation' section, an example is given,
// Thread 1:
r1 = y.load(std::memory_order_relaxed); // A
x.store(r1, std::memory_order_relaxed); // B
// Thread 2:
r2 = x.load(std::memory_order_relaxed); // C
y.store(42, std::memory_order_relaxed); // D
and it says
...In particular, this may occur if D is completed before C in thread 2, either due to compiler reordering or at runtime
I'm wondering that as C is sequenced-before D, how could compiler reorder C and D?
From https://en.cppreference/w/cpp/language/eval_order, the definition of sequenced-before guarantees that both value computation and side effect of an expression A will be completed before another expression B if A is sequenced-before B.
Share Improve this question asked Feb 23 at 12:58 SZYooSZYoo 4372 silver badges9 bronze badges1 Answer
Reset to default 2In C++, sequenced-before defines the evaluation order within a thread, but it does not guarantee that the actual execution order of independent operations will match the source code order when using relaxed memory ordering.
With std::memory_order_relaxed
, no synchronization or ordering guarantees are imposed between atomic operations. The compiler/CPU may freely reorder operations on different variables if there are no data dependencies.
For example:
// Thread 2:
r2 = x.load(std::memory_order_relaxed); // C
y.store(42, std::memory_order_relaxed); // D
Operations C (load from x) and D (store to y) are independent:
- C does not affect the value stored in D (no data dependency).
- The compiler/CPU can reorder D before C because the reordering is invisible to the thread itself (no observable change in behavior).
To conclude my explanation: Sequenced-before ensures intra-thread consistency but does not prevent the compiler/CPU from reordering independent operations when using relaxed atomics. This allows D to complete before C in Thread 2, even though C is sequenced-before D in the source code.
本文标签:
版权声明:本文标题:c++ - Will compiler modify the completion order of a expression pair that has a sequenced-before order? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745154361a2645079.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论