C ++ 11原子x86内存排序 [英] C++11 atomic x86 memory ordering

查看:57
本文介绍了C ++ 11原子x86内存排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 0x中关于原子变量的文档之一中,当描述内存顺序时,它提到:

In one of the docs for atomic variables in C++0x, when describing memory order, it mentions:

发布获取订单

在强序系统(x86,SPARC,IBM大型机)上,发布获取顺序为自动的.对于此同步模式,不会发出其他CPU指令,而仅发出某些编译器优化会受到影响...

On strongly-ordered systems (x86, SPARC, IBM mainframe), release-acquire ordering is automatic. No additional CPU instructions are issued for this synchronization mode, only certain compiler optimizations are affected...

首先是真的,x86遵循严格的内存排序吗?似乎总是强制执行此操作效率很低.意味着每个读写操作都有栅栏吗?

First is it true, that x86 follows strict memory ordering? Seems very inefficient to always impose this. Means every write and read has a fence?

此外,如果我在x86系统上具有对齐的int,原子变量是否有任何用途?

Also, if I have an aligned int, on an x86 system, do the atomic variables serve any purpose at all?

推荐答案

是的,x86确实具有严格的内存排序,请参阅

Yes, it's true that x86 has strict memory ordering, see Volume 3A, Chapter 8.2 of the Intel manuals. Older x86 processors such as the 386 provided truly strict ordering (called strong ordering) semantics, while more modern x86 processors have slightly relaxed conditions in a few cases, but nothing you need to worry about. For example, the Pentium and 486 allow read cache misses to go ahead of buffered writes when the writes are cache hits (and are therefore to different addresses from the reads).

是的,它可能效率很低.因此,有时高性能软件仅针对具有更宽松的内存排序要求的其他体系结构编写.

Yes, it can be inefficient. Sometimes high-performance software is written only for other architectures with looser memory ordering requirements because of this.

是的,原子变量在x86上仍然有用.它们在编译器中具有特殊的语义,因此典型的 read-modify-write 操作原子发生.如果您有两个线程同时增加一个原子变量(我的意思是在C ++ 11中类型为 std :: atomic< T> 的变量),则可以确保该值将为2更大如果没有 std :: atomic ,您可能会得到错误的值,因为即使执行x86的原子存储,一个线程也会在执行增量操作时将当前值缓存在寄存器中.

Yes, atomic variables still serve a purpose on x86. They have special semantics with the compiler such that a typical read-modify-write operation happens atomically. If you have two threads incrementing an atomic variable (by which I mean a variable of type std::atomic<T> in C++11) simultaneously, you can be assured that the value will be 2 larger; without std::atomic, you might end up with the wrong value because one thread cached the current value in a register while performing the increment, even though the store to memory is atomic on x86.

这篇关于C ++ 11原子x86内存排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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