A student in the computer lab writes a small C program to count things. She wants to use all eight cores of the lab machine, so she starts eight threads, and each thread gets a counter of its own: thread 0 adds to counter 0, thread 1 to counter 1, and so on. Nothing is shared. No thread ever looks at another thread's counter, so no lock is needed. One thread adding 1 a hundred million times takes about 20 milliseconds on her machine. Eight threads should take about the same, because every core has its own work. They take 349 milliseconds, almost 18 times longer.
She changes nothing in the arithmetic. She only moves the eight counters apart, so that each one sits 64 bytes after the last. Now the very same program takes 31 milliseconds. Same machine, same additions, same answers. Only the addresses changed.
Where did 318 milliseconds go? Into the caches. Each core has caches of its own, and when one core writes, the hardware must make sure that no other core goes on reading an old copy. It does that for a whole 64-byte line of memory at a time, not for one variable. Eight counters in one line are eight cores fighting over one object. We will measure this in the last part and see exactly why.
To get there we must settle three questions that every cache design answers. What does a cache do when the processor writes? How do several caches, one behind the other, share the work? And how do the caches of different cores stay in agreement? Each of the three can turn up in an answer about cache memory. The Bangladesh Bank Assistant Maintenance Engineer paper of 2011 asked "Explain snooping cache", and papers keep asking for a short note on cache memory, where a good answer needs the ideas of this chapter.