Code Room
Code reviewHardcr-g227
Subject Race conditionsLevel Senior–Staff~40 minCommon in Concurrency interviewsIndustries Software development

Question

Review this C++ lock-free Treiber stack pop.

A reviewer says this lock-free stack has a classic bug. Which one, and how does it manifest?

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
template <class T>struct Node { T val; Node* next; }; std::atomic<Node<int>*> head; int pop() {    Node<int>* old = head.load(std::memory_order_acquire);    while (old &&           !head.compare_exchange_weak(old, old->next,   // (1)                                       std::memory_order_acq_rel)) {    }    int v = old->val;    delete old;                                            // (2) free immediately    return v;}
Run or narrate your approach, then ask the coach.