False sharing occurs when different threads repeatedly update different data items that occupy the same cache line. The items are not shared, but the cache line is: ownership must move between processors as each thread writes. This cache ping-pong can slow work even when every thread has its own variable. section 8.2.3 section 8.2.2
One array entry per thread
Suppose each thread repeatedly updates its own entry in an array of ints. Several entries may fit in one cache line. An update to entry 0 can therefore force a line transfer before another processor updates entry 1, despite the threads never accessing each other’s entries. section 8.2.3
Arrange work and data together:
- Give nearby data items to the same thread; keep items written by different threads far enough apart to avoid sharing a cache line. section 8.2.3 section 8.3.2
- For row-contiguous matrices, assigning adjacent rows to a thread gives it a contiguous write region. Assigning adjacent columns can also reduce false sharing, but writes by different threads still occur within rows. section 8.3.1
struct my_data
{
data_item1 d1;
data_item2 d2;
char padding[std::hardware_destructive_interference_size];
};
my_data some_array[256];
A padded data structure illustrates one way to space array elements apart. C++17’s std::hardware_destructive_interference_size, defined in <new>, provides a target-specific guide to the separation needed to avoid false sharing. section 8.3.2 section 8.2.3
Spacing everything apart is not the goal. Keeping data used by one thread close together can improve data proximity; separating it may increase cache use. Choose the layout and thread assignments together, especially when moving beyond arrays to structures such as trees. section 8.3 section 8.3.2

