Two Independent Counters, No Speedup

Four threads each increment their own counter in an array. There is no lock and no shared variable, and the parallel version is slower than the sequential one. Why?

The scenario

A C++ histogram routine gives each of 8 threads its own uint64_t bucket in a std::array<uint64_t, 8>. Single-threaded it takes 1.2s; with 8 threads it takes 3.8s. There are no locks and no atomics.

What it tests

  • Whether the candidate knows the coherence unit is a cache line, not a variable
  • Whether they can identify false sharing from the symptom rather than only define it
  • Whether they know the remedies and what each costs

Answer it out loud first. Recognising a good answer is not the same skill as producing one.