False Sharing and Atomic Variables

后端 未结 3 543
栀梦
栀梦 2021-01-12 07:21

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 ac

3条回答
  •  再見小時候
    2021-01-12 07:52

    will putting atomic variables in the same cache line make application slower than not putting them in the same cache line?

    False sharing of "atomic" variables could lead to performance problems (whether or not it will lead to such problems depends on a lot of things).

    Let's say you have two cores, A and B, and each operates on its own variable. Let's call these variables a and b respectively.

    A has a in its cache, and B has b in its cache.

    Consider what happens when A increments a.

    • if a and b share a cache line, B's copy of b will get invalidated, and its next access to b will incur a cache miss.
    • if a and b don't share a cache line, there's no impact on B as far as its cached copy of b is concerned.

    This happens regardless of whether a and b are "atomic".

提交回复
热议问题