假共享和原子变量 [英] False Sharing and Atomic Variables

查看:129
本文介绍了假共享和原子变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不同的变量是相同的缓存行里面,你可以体验伪共享,这意味着即使两个不同的线程(在不同内核上运行)正在访问两个不同的变量,如果两个变量驻留在同一高速缓存行,你将有性能损失,因为每一次高速缓存一致性将被触发。

When different variables are inside the same cache line, you can experience False Sharing, which means that even if two different threads (running on different cores) are accessing two different variables, if those two variables reside in the same cache line, you will have performance hit, as each time cache coherence will be triggered.

现在说这些变量是原子变量(原子我的意思是它引入一个存储栅栏的变量,如原子< T> 的C ++),将假共享事那里,或者它并不重要,如果原子变量都在同一高速缓存行还是不行,因为据说他们无论如何都会引入缓存一致性。换句话说,将投入原子变量在同一高速缓存行提出申请慢于不把他们在同一高速缓存行?

Now say those variables are atomic variables (By atomic I mean variables which introduce a memory fence, such as the atomic<t> of C++), will false sharing matter there, or it does not matter if atomic variables are in the same cache line or not, as supposedly they will introduce cache coherence anyway. In other words, will putting atomic variables in the same cache line make application slower than not putting them in the same cache line?

推荐答案

一个澄清:对消极后果至少有一些访问错误共享的变量应该是写入。如果写是罕见的,虚假的共享性能的影响是相当微不足道;越写(等高速缓存行无效消息)性能越差。

A clarification: for negative consequences at least some accesses to "falsely shared" variables should be writes. If writes are rare, performance impact of false sharing is rather negligible; the more writes (and so cache line invalidate messages) the worse performance.

即使使用原子能,高速缓存行共享(无论是或真或假)仍然很重要。看看这里的一些证据:<一href=\"http://www.1024cores.net/home/lock-free-algorithms/first-things-first\">http://www.1024cores.net/home/lock-free-algorithms/first-things-first.因此,答案是 - 是的,将使用不同的线程在同一高速缓存行可能使应用程序速度较慢相比,他们把两个不同的线路原子变量。不过,我认为这将是大多被忽视,除非应用程序花费它的时间更新这些原子变量显著部分。

Even with atomics, cache line sharing (either false or true) still matters. Look for some evidence here: http://www.1024cores.net/home/lock-free-algorithms/first-things-first. Thus, the answer is - yes, placing atomic variables used by different threads to the same cache line may make application slower compared to placing them to two different lines. However, I think it will be mostly unnoticed, unless the app spends a significant portion of its time updating these atomic variables.

这篇关于假共享和原子变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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