Sixteen Bytes Apart
Two threads pinned to different cores each hammer one field of this struct ten million times. The cache line is 64 bytes and the struct is 64-byte aligned.
struct counters { long a; long b; }; /* 16 bytes */
static struct counters c;
void *worker_a(void *_) { for (long i = 0; i < 10000000; i++) c.a++; return 0; }
void *worker_b(void *_) { for (long i = 0; i < 10000000; i++) c.b++; return 0; }
Both final values are exactly correct, but the program runs several times slower than the same loops on two separate structs. Why?
Sign in to answer questions and track your progress
Sign In