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
