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

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

问题描述

任何人都可以用简单的英语解释什么是std :: memory_order,以及如何使用std :: atomic<>?



我发现引用和少数这里的例子,但不了解。
http://en.cppreference.com/w/cpp/atomic/memory_order



感谢。

解决方案


解释什么是std :: memory_order在简单的英语,


我找到的最好的普通英语是Bartoz Milewski的关于轻松原子的文章: http://bartoszmilewski.com/2008/12 / 01 / c-atomics-and-memory-ordering /



后续帖子: http://bartoszmilewski.com/2008/12/23/the-inscrutable-c-memory-model/ 但是请注意,虽然这些文章是一个很好的介绍,他们预先的C ++ 11标准,并不会告诉你需要知道的一切安全使用它们。


如何使用std :: atomic<>?


我最好的建议是:不要。轻松的原子是(可能)最棘手和最危险的事情在C + + 11。坚持 std :: atomic< T> 与默认内存排序(顺序一致性),直到你真的,真的确定你有一个性能问题,可以解决使用宽松的内存排序。



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


我不知道我在尝试推理
关于C ++弱原子的时候自己陷入了什么。它们背后的理论是如此复杂,以至于
边界不可用。需要三个人(Anthony,Hans和我)和
修改标准以完成相对
简单算法的证明。想象一下,对于弱原子的基于
的无锁队列也是这样做的!



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

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

Thanks.

解决方案

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

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/

And the follow-up post: http://bartoszmilewski.com/2008/12/23/the-inscrutable-c-memory-model/

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.

and how to use them with 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.

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

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天全站免登陆