Multicore & Synchronization

Which describes false sharing in a multithreaded program?

  1. AThreads update different variables that sit in the same cache line, so the line bounces between cores and slows things down
  2. BTwo threads update the same variable without synchronization and corrupt it
  3. CThreads wait on each other's locks and stop
Show answer and explanation

Answer: A. Threads update different variables that sit in the same cache line, so the line bounces between cores and slows things down

Coherence is managed per cache line, typically 64 bytes. Even if core 0 writes only a and core 1 writes only b, when a and b share a line each write invalidates the other's copy and the line bounces between the cores. The results are correct but performance drops sharply: this is false sharing. It often occurs with arrays of per-thread counters.

  • A. Correct. The data is not shared, but the line is.
  • B. That is a data race.
  • C. That is a deadlock.
Practice this topic Back to topic

More Multicore & Synchronization questions

  1. False sharing occurs with an array of per-thread counters such as "long cnt[8];". What is the most suitable fix?
  2. Which describes a data race?
  3. When two threads run the code above simultaneously, the final counter can be less than 200000. Why?
  4. Which describes an atomic instruction?
  5. What does the atomic CAS (compare-and-swap) instruction do?