c++,std::atomic,std::memory_order 是什么以及如何使用它们? [英] c++, std::atomic, what is std::memory_order and how to use them?

查看:56
本文介绍了c++,std::atomic,std::memory_order 是什么以及如何使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能用简单的英语解释什么是std::memory_order,以及如何将它们与std::atomic<>一起使用?

Can anyone explain what is std::memory_order in plain English, and how to use them with std::atomic<>?

我在这里找到了参考资料和几个例子,但根本不明白.http://en.cppreference.com/w/cpp/atomic/memory_order

I found the reference and few examples here, but don't understand at all. http://en.cppreference.com/w/cpp/atomic/memory_order

推荐答案

谁能用简单的英语解释一下 std::memory_order 是什么,

Can anyone explain what is std::memory_order in plain English,

我为各种内存排序找到的最好的简单英语"解释是 Bartoz Milewski 的关于轻松原子的文章:http://bartoszmilewski.com/2008/12/01/c-atomics-and-memory-ordering/

The best "Plain English" explanation I've found for the various memory orderings is Bartoz Milewski's article on relaxed atomics: http://bartoszmilewski.com/2008/12/01/c-atomics-and-memory-ordering/

以及后续帖子:http://bartoszmilewski.com/2008/12/23/the-inscrutable-c-memory-model/

但请注意,虽然这些文章是一个很好的介绍,但它们早于 C++11 标准,并且不会告诉您安全使用它们需要知道的一切.

But note that whilst these articles are a good introduction, they pre-date the C++11 standard and won't tell you everything you need to know to use them safely.

以及如何将它们与 std::atomic<> 一起使用?

and how to use them with std::atomic<>?

我最好的建议是:不要.松弛原子(可能)是 C++11 中最棘手和最危险的事情.坚持使用带有默认内存排序(顺序一致性)的 std::atomic ,直到您真的非常确定您有性能问题可以通过使用宽松的内存排序来解决.

My best advice to you here is: don't. Relaxed atomics are (probably) the trickiest and most dangerous thing in C++11. Stick to std::atomic<T> with the default memory ordering (sequential consistency) until you're really, really sure that you have a performance problem that can be solved by using the relaxed memory orderings.

在上面链接的第二篇文章中,Bartoz Milewski 得出以下结论:

In the second article linked above, Bartoz Milewski reaches the following conclusion:

我不知道自己在尝试推理时陷入了什么境地关于 C++ 弱原子.它们背后的理论是如此复杂以至于边缘无法使用.花了三个人(安东尼、汉斯和我)和对标准的修改,以完成一个相对的证明简单的算法.想象一下对基于无锁队列的关于弱原子!

I had no idea what I was getting myself into when attempting to reason about C++ weak atomics. The theory behind them is so complex that it’s borderline unusable. It took three people (Anthony, Hans, and me) and a modification to the Standard to complete the proof of a relatively simple algorithm. Imagine doing the same for a lock-free queue based on weak atomics!

这篇关于c++,std::atomic,std::memory_order 是什么以及如何使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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