Multicore & Synchronization

When several threads sum all elements of an array, which approach performs well?

  1. AEvery thread adds each element to one shared variable, taking a lock each time
  2. BEvery thread adds each element to one shared variable without synchronization
  3. CEach thread sums its share in a local variable, and the partial sums are added once each at the end
Show answer and explanation

Answer: C. Each thread sums its share in a local variable, and the partial sums are added once each at the end

Updating a shared variable per element is very slow because of lock or atomic waits and cache lines bouncing between cores. If each thread computes a partial sum in a register or local variable and the total is formed with just one addition per thread at the end, shared writes become negligible. This approach is called a reduction and is built into parallel frameworks such as OpenMP.

  • A. Lock waits and communication nearly eliminate the benefit.
  • B. A data race makes the total wrong.
  • C. Correct. This is called a reduction.
Practice this topic Back to topic

More Multicore & Synchronization questions

  1. What is a characteristic of a reader-writer lock?
  2. What is the most suitable use of a counting semaphore?
  3. What is the difference between cache coherence and a memory consistency model?
  4. Which describes the idea of hardware transactional memory?
  5. When waiting on a condition variable until a queue becomes non-empty, why is the condition rechecked in a while loop rather than an if after waking?