Yes, if you write to two adjacent int
elements inside a std::vector
, it is likely that they will both be on the same cache line, which will cause false sharing if this cache line is accessed simultaneously by two different threads.
C++17 introduced std::hardware_destructive_interference_size
, which is a portable way to get a hint from the compiler on what the L1 cache line size is expected to be on the target platform.
Therefore, to prevent false sharing, you should ensure that the two int
variables are at least std::hardware_destructive_interference_size
bytes apart:
void func() {
constexpr int min_offset = std::hardware_destructive_interference_size / sizeof(int);
std::vector<int> res( min_offset + 1, 0 );
std::thread t1( task1, &res[0] );
std::thread t2( task2, &res[min_offset] );
t1.join();
t2.join();
return res[0] + res[min_offset];
}
However, at the time of this writing, several compilers do not (yet) support std::hardware_destructive_interference_size
. See this question for further information.
If you want to be reasonably certain that your code will not have false-sharing in the distant future, then you may want to assume that the cache size is double the size reported by std::hardware_destructive_interference_size
.