Kolmapäev, juuli 29, 2026

Ajalugu, Täna ajaloos

TÄNA AJALOOS, 11. juuni ⟩ Hävitati Trooja linn

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 (ABC). 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 (AC). 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. 1 2 3 Williams, Anthony (2012). "7.2, "Examples of lock-free data structures"". C++ Concurrency in Action: Practical Multithreading. Shelter Island: Manning.
  2. 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)
  3. US patent 20040107227, Michael, Maged M., "Method for efficient implementation of dynamic lock-free data structures with safe memory reclamation", issued 3 June 2004
  4. "Standard library header <hazard_pointer> (C++26)". cppreference. Retrieved 7 November 2025.

Further reading

Lisa kommentaar

Sinu e-postiaadressi ei avaldata. Nõutavad väljad on tähistatud *-ga