How do hazard pointers make lock-free memory reclamation safe, and when are they preferable to reference counting or epoch-based reclamation?
Show answer and explanation
Before dereferencing a shared candidate, a reader publishes its address in an owned hazard slot, reloads the shared pointer, and retries unless it is still the same. A remover unlinks a node but retires rather than deletes it; reclamation occurs only after scanning hazard slots proves no reader protects that node. Publication order, slot ownership, scan order, and delayed retire lists are part of the algorithm, so use a proven implementation where possible. Hazard pointers provide per-node reclamation, and a stalled reader delays only nodes it still protects, but they add scans and bookkeeping. Reference counting is simpler for shared ownership but adds atomic updates and cycle concerns; epochs batch cheaply but a stalled participant can delay all reclamation.
Before dereferencing a shared candidate, a reader publishes its address in an owned hazard slot, reloads the shared pointer, and retries unless it is still the same. A remover unlinks a node but retires rather than deletes it; reclamation occurs only after scanning hazard slots proves no reader protects that node. Publication order, slot ownership, scan order, and delayed retire lists are part of the algorithm, so use a proven implementation where possible. Hazard pointers provide per-node reclamation, and a stalled reader delays only nodes it still protects, but they add scans and bookkeeping. Reference counting is simpler for shared ownership but adds atomic updates and cycle concerns; epochs batch cheaply but a stalled participant can delay all reclamation.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.