Code Room
Code reviewHardcr-g224
Subject False sharingLevel Senior–Staff~40 minCommon in Code quality & review interviewsIndustries Software development

Question

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.

Talk through your review
Code to reviewcpp
struct Counters {    std::atomic<long> hits[8];   // one slot per worker thread}; void worker(Counters& c, int tid, const std::vector<int>& work) {    for (int v : work) {        if (heavy_predicate(v))            c.hits[tid].fetch_add(1, std::memory_order_relaxed);  // (1)    }}
Run or narrate your approach, then ask the coach.