ArticleslgStudy

science

Hazard pointer

Hazard pointer is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Hazard pointer rather than just read about it. In short: 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.

Key takeaways

  • Hazard pointer belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Hazard pointer to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Hazard pointer from memory before moving on to harder problems.

Reference excerpt

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. 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

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 or two elements.) Nodes on the hazard pointer list must not be modified or deallocated by any other thread.

Each reader thread owns a single-writer/multi-reader shared pointer called "hazard pointer." When a reader thread assigns the address of a map to its hazard pointer, it is basically announcing to other threads (writers), "I am reading this map. You can replace it if you want, but don't change its contents and certainly keep your deleteing hands off it." 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 <hazard_pointer>, for C++ Standard Library threading support. To be hazard-protectable, a class must extend a class called std::hazard_ptr_obj_base<T, Del> (where the template parameter Del is by default std::default_delete<T>).

See also Concurrent data structure Hazard (computer architecture) Finalizer

References

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. 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

Worked examples

Example 1 — a first encounter with Hazard pointer

Start with the simplest possible case. Write down what Hazard pointer claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Hazard pointer before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Hazard pointer ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Hazard pointer

In research
Hazard pointer appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Hazard pointer in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Hazard pointer is common in secondary-school and first-year university syllabi. It links to neighbouring topics Programming constructs, so understanding it makes those chapters shorter.
In everyday life
Look for Hazard pointer outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Hazard pointer” →

How to study Hazard pointer in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Hazard pointer means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Hazard pointer out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Hazard pointer in simple terms?

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.

Why does Hazard pointer matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Hazard pointer?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Hazard pointer.

Tags

  • Programming constructs

Keep exploring