Hard
A `lock add` on an operand that straddles two cache lines
g_stats is 64-byte aligned, and the packed layout puts hits at bytes 60 through 67, straddling a cache-line boundary. On an x86-64 server where split-lock detection is disabled (or the CPU predates it), one thread calls record() in a tight loop while other cores run unrelated, memory-intensive processes that never touch g_stats. The increment compiles to lock add qword ptr [g_stats+60], 1. What does the hardware do for this instruction, and who pays for it?
main.cpp
#pragma pack(push, 1)
struct Stats {
char tag[60];
uint64_t hits; // offset 60: bytes 60..67
};
#pragma pack(pop)
alignas(64) Stats g_stats;
void record() {
__atomic_fetch_add(&g_stats.hits, 1, __ATOMIC_RELAXED);
}46 of 47 in Computer Architecture