language-icon Old Web
English
Sign In

Hazard pointer

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

[ "Garbage collection", "Manual memory management", "Memory leak" ]
Parent Topic
Child Topic
    No Parent Topic