liveBook · Manning

Cache ping-pong

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

Cache ping-pong is performance-degrading cache-coherence traffic caused by multiple threads repeatedly accessing the same shared data.

Cache ping-pong is repeated cache-data transfer between processors when threads access shared data. These transfers can stall processors and reduce application performance, especially under high contention. section 8.2.2

std::atomic<unsigned long> counter(0);
void processing_loop()
{
    while(counter.fetch_add(1,std::memory_order_relaxed)<100000000)
    {
        do_something();
    }
}

A shared atomic counter is updated in a loop; repeated access can make its data ping-pong between caches. section 8.2.2

Mutexes can also ping-pong: locking and unlocking transfers mutex data between processors. Separately, false sharing causes similar traffic when threads update different variables that occupy the same cache line. section 8.2.2 section 8.2.3

std::mutex m;
my_data data;
void processing_loop_with_mutex()
{
    while(true)
    {
        std::lock_guard<std::mutex> lk(m);
        if(done_processing(data)) break;
    }
}

The loop repeatedly acquires a mutex while checking shared data, illustrating how mutex traffic can add to contention. section 8.2.2

For false sharing, arrange data accessed by the same thread close together and data accessed by different threads farther apart. Compare lock-free and lock-based designs using the performance measure that matters for the application. section 8.2.3 section 7.1.4

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

How deep it goes
Traces transfers through counter and mutex examples, then connects shared cache lines to false sharing and data layout.
Examples
Uses a shared atomic counter, a mutex-protected loop, and threads updating separate array entries on one cache line.
What you can do after
Diagnose potential performance drains from shared atomic or mutex access and compare lock-free and lock-based designs.
How it connects
Connects data contention, atomic variables, mutexes, lock-free data structures, and 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.