What is false sharing, and how can you use alignas to prevent it in a multithreaded environment?

Last updated 21 June 2026

  • False sharing: threads writing different variables that sit on the same cache line (typically 64 bytes) force the line to bounce between cores via cache coherency, hurting performance.

  • Fix: use alignas to place each hot, independently-written variable on its own cache line, ideally with std::hardware_destructive_interference_size (C++17) instead of hardcoding 64.

  • Trade-off: the padding wastes memory, so apply it only to genuinely hot, concurrently-written data.

  • https://dev.to/media_geneous_bb80d6b5563/c-tips-and-tricks-for-advanced-developers-33ko
  • https://dev.to/hamzahassanain0/what-every-programmer-should-know-about-memory-part-4-4bh5
  • https://souvik150.medium.com/false-sharing-the-hidden-performance-killer-in-multithreaded-code-1409c265d0e5
All C++ interview questions