false是否与堆内存共享案例?

发布于 2025-02-03 20:31:09 字数 447 浏览 2 评论 0原文

众所周知, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就此别过 2025-02-10 20:31:09

内存是内存。在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文