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
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
.
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.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".