Search code examples
multithreadingshared-memorycpu-architecturehardware-acceleration

Can multiple threads access shared memory at the same time?


Multiple threads running in parallel on several cores of a CPU. Can they access the main memory at the same time?


Solution

  • Main memory and shared last-level-cache read bandwidth is a shared resource that multiple cores compete for, but yes, multiple readers reading the same byte of memory will typically complete faster than multiple readers reading from separate pages. (Not true with writes in the mix.)

    If the shared-memory region is small enough that it's hot in each core's private cache, then each core can read from it at very high speeds. Writing will slow down other readers and esp. other writers (cf. Are cache-line-ping-pong and false sharing the same?).

    Other readers will not be slowed down very much if they don't use any kind of locking, instead relying on lockless algorithms to avoid errors due to race conditions. This is why lockless programming is sometimes worth the (large) challenge of getting it correct, compared to just using locking or producer-consumer flags.