From Surf Wiki (app.surf) — the open knowledge base
Hazard pointer
Programming concept
Programming concept
In a multithreaded computing environment, hazard pointers 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.
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
compare_and_swap(target=&head, newvalue=B, expected=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* currentNode = this->head; // assume the load from "this->head" is atomic
Node* nextNode = currentNode->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 this-head and deallocate it, meaning that the memory access through currentNode 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 or two elements.) Nodes on the hazard pointer list must not be modified or deallocated by any other thread.
When a thread wishes to remove a node, it places it on a list of nodes "to be freed later", but does not actually deallocate the node's memory until no other thread's hazard list contains the pointer. This manual garbage collection can be done by a dedicated garbage-collection thread (if the list "to be freed later" is shared by all the threads); alternatively, cleaning up the "to be freed" list can be done by each worker thread as part of an operation such as "pop" (in which case each worker thread can be responsible for its own "to be freed" list).
In 2002, Maged Michael of IBM filed an application for a U.S. patent on the hazard pointer technique, but the application was abandoned in 2010.
Alternatives to hazard pointers include reference counting.
Hazard pointers were added to C++26 as std::hazard_pointer which is a single-writer multi-reader pointer that can be owned by at most one thread at any point of time, in header ``, for C++ Standard Library threading support. To be hazard-protectable, a class must extend a class called std::hazard_ptr_obj_base.
References
References
- Anthony Williams. ''C++ Concurrency in Action: Practical Multithreading.'' Manning:Shelter Island, 2012. See particularly Chapter 7.2, "Examples of lock-free data structures".
- Andrei Alexandrescu and Maged Michael. (2004). "Lock-Free Data Structures with Hazard Pointers". Dr. Dobb's.
- {{patent. US. 20040107227. application. Maged M. Michael, "Method for efficient implementation of dynamic lock-free data structures with safe memory reclamation." Filed on 3 December 2002.
- "Standard library header <hazard_pointer>". cppreference.
This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.
Ask Mako anything about Hazard pointer — get instant answers, deeper analysis, and related topics.
Research with MakoFree with your Surf account
Create a free account to save articles, ask Mako questions, and organize your research.
Sign up freeThis content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.
Report