线程访问相同的缓存行 [英] threads accessing same cache line

查看:72
本文介绍了线程访问相同的缓存行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个建议,即线程不要访问相同的缓存行,我真的不明白为什么,而且在搜索该主题时,我遇到了以下问题: 多个线程和CPU缓存 答案之一建议:

I came across a suggestion for threads not to access same cache lines and I really cant understand why, also while doing a search on that topic I came around with this questions: Multiple threads and CPU cache where one of the answers suggested:

您只想避免两个线程同时尝试访问位于同一高速缓存行上的数据

you just want to avoid two threads from simultaneously trying to access data that is located on the same cache line

以我的方式看,缓存存储了内存页面以从进程中快速访问,并且它在这里说:

The way I see it, the cache stores pages of memory for quick access from the process, And as it says here:http://en.wikipedia.org/wiki/Thread_%28computing%29#How_threads_differ_from_processes

线程共享其地址空间

threads share their address space

两个线程访问相同的缓存行应该不是问题,因为如果页面在缓存中并且尝试访问内存的线程将获得缓存命中,而与其他线程无关.

It shouldn't be a problem for two threads to access same cache line since if a page is in the cache and a thread trying to access the memory will get a cache hit regardless of the other thread.

我听到了关于避免线程在几种不同情况下访问同一缓存行的争论,所以这不是一个神话.我在这里想念什么?

I heard the argument about avoiding threads from accessing same cache line on several different occasions so it cant be a myth. What am I missing here?

推荐答案

在大多数(可能是所有硬件,但我还没有详尽的硬件知识)多核CPU中,当一个内核尝试写入时,缓存将锁定当前访问的行进入相应的内存.因此,其他尝试访问同一缓存行的内核将处于等待状态.

In most (probably all but I don't have an exhaustive hardware knowledge) multicore CPUs, the cache will lock the currently accessed line when one core tries to write into the corresponding memory. So other cores trying to access the same cache line will be held in wait.

只要线程是只读的(或不经常更新的),就可以在线程之间共享相同的数据,但是如果您继续对其进行写入,则隐藏访问序列化将产生与在同一内核上运行所有线程等效的性能(实际上是由于缓存锁定延迟而使情况变得更糟).

You can share the same data among threads as long as it's read only (or infrequently updated), but if you keep writing into it, the hidden access serialization will yield performances equivalent to running all threads on the same core (actually a bit worse due to cache locking delays).

这篇关于线程访问相同的缓存行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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