加载/存储松弛原子和普通变量之间有什么区别? [英] What is the difference between load/store relaxed atomic and normal variable?

查看:91
本文介绍了加载/存储松弛原子和普通变量之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个测试用例中看到: https://godbolt.org/z/K477q1

As I see from a test-case: https://godbolt.org/z/K477q1

生成的程序集加载/存储原子松弛与普通变量相同:ldr和str

The generated assembly load/store atomic relaxed is the same as the normal variable: ldr and str

那么,弛豫原子变量和普通变量之间有什么区别吗?

So, is there any difference between relaxed atomic and normal variable?

推荐答案

区别在于,正常加载/存储不能 保证 无撕裂,而轻松的原子读/写是.同样,原子保证编译器不会以与 volatile 保证类似的方式重新安排或优化内存访问.

The difference is that a normal load/store is not guaranteed to be tear-free, whereas a relaxed atomic read/write is. Also, the atomic guarantees that the compiler doesn't rearrange or optimise-out memory accesses in a similar fashion to what volatile guarantees.

(C ++ 11之前的版本, volatile 是滚动您自己的原子的重要组成部分.但是现在它已经过时了.它在实践中仍然有效,但是从不建议这样做:何时在多线程中使用volatile?-从不.)

(Pre-C++11, volatile was an essential part of rolling your own atomics. But now it's obsolete for that purpose. It does still work in practice but is never recommended: When to use volatile with multi threading? - essentially never.)

在大多数平台上,碰巧该体系结构默认情况下会提供无撕裂的加载/存储(对于对齐的 int long ),因此在asm if 的加载和存储没有得到优化.参见为什么对自然分配整数例如在x86上对齐原子变量?.在C ++中,由您决定如何在源代码中访问内存,而不是依靠体系结构特定的功能来使代码按预期工作.

On most platforms it just happens that the architecture provides a tear-free load/store by default (for aligned int and long) so it works out the same in asm if loads and stores don't get optimized away. See Why is integer assignment on a naturally aligned variable atomic on x86? for example. In C++ it's up to you to express how the memory should be accessed in your source code instead of relying on architecture-specific features to make the code work as intended.

如果您是用asm手写的,那么将值保存在寄存器中还是将值保存/加载/存储到(共享)内存中时,您的源代码就已经确定下来.在C ++中,告诉编译器何时/不能将值保持私有是为什么 std :: atomic< T> 存在的一部分.

If you were hand-writing in asm, your source code would already nail down when values were kept in registers vs. loaded / stored to (shared) memory. In C++, telling the compiler when it can/can't keep values private is part of why std::atomic<T> exists.

如果您阅读有关此主题的一个文章,请查看此处的Preshing文章: https://preshing.com/20130618/atomic-vs-non-atomic-operations/

If you read one article on this topic, take a look at the Preshing one here: https://preshing.com/20130618/atomic-vs-non-atomic-operations/

也可以尝试CppCon 2017的以下演示文稿: https://www.youtube.com/watch?v=ZQFzMfHIxng

Also try this presentation from CppCon 2017: https://www.youtube.com/watch?v=ZQFzMfHIxng

链接以供进一步阅读:

https://en.cppreference.com/w/cpp/atomic/memory_order#Relaxed_ordering

导致非原子撕裂

https://lwn.net/Articles/793895/

什么是(轻微)放松的原子规则有什么不同?,其中包括指向Herb Sutter的原子武器"链接文章也链接在这里: https://herbsutter.com/2013/02/11/atomic-weapons-the-c-memory-model-and-modern-hardware/

What is the (slight) difference on the relaxing atomic rules? which includes a link to a Herb Sutter "atomic weapons" article which is also linked here: https://herbsutter.com/2013/02/11/atomic-weapons-the-c-memory-model-and-modern-hardware/

另请参阅Peter Cordes的链接文章: https://electronics.stackexchange.com/q/387181
还有一个有关Linux内核的相关文章: https://lwn.net/Articles/793253/

Also see Peter Cordes' linked article: https://electronics.stackexchange.com/q/387181
And a related one about the Linux kernel: https://lwn.net/Articles/793253/

使用 std :: atomic< T> 所获得的一切都不只是撕裂-您还可以避免数据竞争未定义的行为.

No tearing is only part of what you get with std::atomic<T> - you also avoid data race undefined behaviour.

这篇关于加载/存储松弛原子和普通变量之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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