Home / Forums / Understanding False Sharing in Multi-Threaded C++ Data Structures

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Tutorial

Understanding False Sharing in Multi-Threaded C++ Data Structures

ModernCpp_Dan
C++20 / C++23 Specialist
MEMBER
Rep: 410
Join Date: Oct 2023
Posts: 6
Thanks: 18
2y ago · Oct 1, 2023 5:03 PM
#1
False sharing occurs when two independent threads modify variables that reside on the exact same 64-byte CPU cache line. Even though the variables are distinct, the hardware cache coherence protocol constantly invalidates cache lines across cores.
ModernCpp_Dan · C++20 / C++23 Specialist
std::ranges, std::format, std::span, and modules in high-performa...
The following users thanked ModernCpp_Dan for this post:
StdRangesFan
Functional C++
MEMBER
Rep: 119
Join Date: Mar 2022
Posts: 6
Thanks: 79
2y ago · Oct 1, 2023 6:47 PM
#2
Use alignas(std::hardware_destructive_interference_size) on multi-threaded shared counters or per-thread state structures to ensure they occupy separate cache lines.
StdRangesFan · Functional C++
C++20 view pipelines, lazy evaluation, and composable range algor...
DynamicLinking
PE / ELF Linker Expert
MEMBER
Rep: 79
Join Date: Nov 2024
Posts: 6
Thanks: 102
2y ago · Oct 2, 2023 12:47 AM
#3
Adding proper alignment padding increased our parallel worker queue throughput by over 400% on a 16-core CPU!
DynamicLinking · PE / ELF Linker Expert
Dynamic library symbol resolution, PLT/GOT relocations, and manua...