false是否与堆内存共享案例?
众所周知, false共享会在几个线程尝试读取放置在同一缓存线中的小且相邻的数据时:
#include <omp.h>
#define NUM_THREADS 4
int main() {
int arr[NUM_THREADS];
# pragma omp parallel num_threads(NUM_THREADS)
{
const int id = omp_get_thread_num();
arr[id] // doing something with it
}
}
如果我动态创建数组,该怎么办?
int *arr = new int[NUM_THREADS];
如果我的数组在堆上,会发生错误的共享吗?在这种情况下,是否有一些缓存线限制?
As I know, false sharing occurs when several threads try to read small and adjacent pieces of data which are placed within the same cache line:
#include <omp.h>
#define NUM_THREADS 4
int main() {
int arr[NUM_THREADS];
# pragma omp parallel num_threads(NUM_THREADS)
{
const int id = omp_get_thread_num();
arr[id] // doing something with it
}
}
What if I create the array dynamically?
int *arr = new int[NUM_THREADS];
Will false sharing take place if I have my array on the heap? Are there some cache line restrictions in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
内存是内存。在CPU上,堆栈上的数组与堆上的数组完全相同。因此,任何错误的共享问题都保持不变。
Memory is memory. To the cpu an array on the stack is exactly the same as an array on the heap. So any false sharing problem remains the same.