Hazard pointers, in a multithreaded computing environment, are one approach to solving the problems posed by dynamic memory management of the nodes in a lock-free data structure. These problems generally arise only in environments that don't have automatic garbage collection.[1] Although they are a pointer type, they are not smart pointers.
Any lock-free data structure that uses the compare-and-swap primitive must deal with the ABA problem. For example, in a lock-free stack represented as an intrusively linked list, one thread may be attempting to pop an item from the front of the stack (A → B → C). It remembers the second-from-top value B, and then performs head.compareAndSwap(B, A). Unfortunately, in the middle of this operation, another thread may have done two pops and then pushed A back on top, resulting in the stack (A → C). The compare-and-swap succeeds in swapping head with B, and the result is that the stack now contains garbage (a pointer to the freed element B).
Furthermore, any lock-free algorithm containing code of the form
Node current = x.head(); // assume the load from "x.head()" is atomic
Node next = current.next(); // assume this load is also atomic
suffers from another major problem, in the absence of automatic garbage collection. In between those two lines, it is possible that another thread may pop the node pointed to by x.head() and deallocate it, meaning that the memory access through current on the second line reads deallocated memory (which may in fact already be in use by some other thread for a completely different purpose).
Hazard pointers can be used to address both of these problems. In a hazard-pointer system, each thread keeps a list of hazard pointers indicating which nodes the thread is currently accessing. (In many systems this "list" may be probably limited to only one[1][2] or two elements.) Nodes on the hazard pointer list must not be modified or deallocated by any other thread.
See also
References
- 1 2 3 Williams, Anthony (2012). "7.2, "Examples of lock-free data structures"". C++ Concurrency in Action: Practical Multithreading. Shelter Island: Manning.
- 1 2 Alexandrescu, Andrei; Michael, Maged (2004). "Lock-Free Data Structures with Hazard Pointers". Dr. Dobb's. Archived from the original on 19 July 2019. Retrieved 1 April 2014. (C++ oriented article)
- ↑ US patent 20040107227, Michael, Maged M., "Method for efficient implementation of dynamic lock-free data structures with safe memory reclamation", issued 3 June 2004
- ↑ "Standard library header <hazard_pointer> (C++26)". cppreference. Retrieved 7 November 2025.
Further reading
- Michael, Maged M. (2004). "Hazard Pointers: Safe Memory Reclamation for Lock-Free Objects" (PDF). IEEE Transactions on Parallel and Distributed Systems. 15 (8): 491–504. CiteSeerX 10.1.1.130.8984. doi:10.1109/TPDS.2004.8. S2CID 8373852. Archived from the original (PDF) on 4 November 2017.
External links
- Concurrent Building Blocks - C++ implementation of Hazard Pointer (called "SMR") and other lock-free data structures. Also has Java interfaces.
- "Concurrency Kit". Archived from the original on 1 June 2014. - C implementation of Hazard Pointer and lock-free data structures
- Atomic Ptr Plus - C/C++ library that has a Hazard Pointer implementation
- The parallelism shift and C++'s memory model - Contains C++ implementation for Windows in appendices
- libcds - C++ library of lock-free containers and Hazard Pointer implementation
