White RoomNEW

Sixty Four Bytes Apart

x86-64, 64-byte cache lines, two cores.

struct Counters {
    std::atomic<long> a;
    std::atomic<long> b;
};
Counters c;

// core 0: for (long i = 0; i < 100'000'000; i++) c.a.fetch_add(1);
// core 1: for (long i = 0; i < 100'000'000; i++) c.b.fetch_add(1);

Both final values are exactly 100,000,000, so the program is correct. It runs roughly 5x slower than the same two loops on counters allocated in separate structs. Which change fixes the slowdown?