Lock-free stack pop implementation in C++
The thread that calls try_reclaim
has just removed old_head
from the stack.
The class ensures that any other uses of old_head
must be inside pop
calls from other threads, so if the thread discovers that there are no other concurrent calls, then it knows that it is the exclusive holder of the old_head
pointer. Then, as long as it doesn't publish that pointer so that it might be picked up from another thread, it can delete it whenever it gets around to it.
So the implementation is safe. The question you asked: "Why doesn't he check [again]" indicates that you are thinking about it incorrectly. Checking again wouldn't prove anything, because if it were possible for another thread to get into pop
and use old_head
, then it could always happen after you check!