Code Room
Code reviewMediumcr-g413
Subject Atomicity violationsLevel Mid–Senior~25 minCommon in Concurrency interviewsIndustries Software development

Question

Review this C++ reference-counted handle. `release()` is called from multiple threads when handles go out of scope.

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 Resource {  std::atomic<int> refs{1};  Data* data;   void acquire() { refs.fetch_add(1, std::memory_order_relaxed); }   void release() {    if (refs.fetch_sub(1, std::memory_order_relaxed) == 1) {      delete data;      data = nullptr;    }  }};
Run or narrate your approach, then ask the coach.