Code RoomFalse sharing in atomic array
HardPrep Room Coding #1859

False sharing in atomic array

Code reviewCode quality & reviewSenior–Staff~40 min

Review this C++ per-thread counter array used in a hot parallel loop.

It's correct but a senior reviewer flagged it. What's the concurrency-related problem?

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 40 min
Mark a line and say what kind of problem it is.0 findings
1struct Counters {
2 std::atomic<long> hits[8]; // one slot per worker thread
3};
4 
5void worker(Counters& c, int tid, const std::vector<int>& work) {
6 for (int v : work) {
7 if (heavy_predicate(v))
8 c.hits[tid].fetch_add(1, std::memory_order_relaxed); // (1)
9 }
10}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.