liveBook · Manning

False sharing

As explained in C++ Concurrency in Action, Second Edition

False sharing is a performance problem in which distinct data items accessed by different threads occupy the same cache line, causing unnecessary cache-line transfers.

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

How C++ Concurrency in Action, Second Edition covers it

How deep it goes
Explains cache-line ownership transfers through repeated array updates, then gives spacing and work-partitioning remedies without prescribing a universal layout.
Examples
Uses per-thread array entries, row-versus-column matrix assignments, and a padded C++ structure.
What it assumes
Assumes C++ arrays and threads while explaining cache-line ping-pong through a concrete update scenario.
What you can do after
Diagnose/debug with it by spotting cross-thread writes to one cache line, then regrouping work or spacing data apart.
How it connects
Connects cache ping-pong, contention, data proximity, and work partitioning to the placement of thread-owned data.

Also explained in

Compare how 2 books explain false sharing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C++ Concurrency in Action, Second Edition. The section links point to the parts of the book it draws on.