I came across a suggestion for threads not to access same cache lines and I really cant understand why, also while doing a search on that topic I came around with this questions: Multiple threads and CPU cache where one of the answers suggested:
you just want to avoid two threads from simultaneously trying to access data that is located on the same cache line
The way I see it, the cache stores pages of memory for quick access from the process, And as it says here:http://en.wikipedia.org/wiki/Thread_%28computing%29#How_threads_differ_from_processes
threads share their address space
It shouldn't be a problem for two threads to access same cache line since if a page is in the cache and a thread trying to access the memory will get a cache hit regardless of the other thread.
I heard the argument about avoiding threads from accessing same cache line on several different occasions so it cant be a myth. What am I missing here?
On modern multi-core processors, independent workloads often interfere with each other by competing for shared cache space. However, for multi-threaded workloads, where a single copy of data can be accessed by multiple threads, the threads can cooperatively share cache.
Multiple threads accessing shared data simultaneously may lead to a timing dependent error known as data race condition. Data races may be hidden in the code without interfering or harming the program execution until the moment when threads are scheduled in a scenario (the condition) that break the program execution.
In a multi-threaded process, all of the process' threads share the same memory and open files. Within the shared memory, each thread gets its own stack. Each thread has its own instruction pointer and registers.
Threads are sometimes called lightweight processes because they have their own stack but can access shared data. Because threads share the same address space as the process and other threads within the process, the operational cost of communication between the threads is low, which is an advantage.
In most (probably all but I don't have an exhaustive hardware knowledge) multicore CPUs, the cache will lock the currently accessed line when one core tries to write into the corresponding memory. So other cores trying to access the same cache line will be held in wait.
You can share the same data among threads as long as it's read only (or infrequently updated), but if you keep writing into it, the hidden access serialization will yield performances equivalent to running all threads on the same core (actually a bit worse due to cache locking delays).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With