{"id":10682,"date":"2025-03-10T11:42:17","date_gmt":"2025-03-10T11:42:17","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=10682"},"modified":"2025-07-03T14:37:49","modified_gmt":"2025-07-03T14:37:49","slug":"a-lock-free-stack-a-hazard-pointer-implementation","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/a-lock-free-stack-a-hazard-pointer-implementation\/","title":{"rendered":"A Lock-Free Stack: A Hazard Pointer Implementation"},"content":{"rendered":"\n<p>Hazard Pointers solve all issues of the previous implementation: <a href=\"https:\/\/www.modernescpp.com\/index.php\/a-lock-free-stack-a-simple-garbage-collector\/\">A Lock-Free Stack: A Simple Garbage Collector<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"711\" height=\"491\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/01\/Time26Concurrency.png\" alt=\"\" class=\"wp-image-10580\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/01\/Time26Concurrency.png 711w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/01\/Time26Concurrency-300x207.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/01\/Time26Concurrency-705x487.png 705w\" sizes=\"auto, (max-width: 711px) 100vw, 711px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">From one Week to two Weeks <\/h2>\n\n\n\n<p>You may have already noticed. I will change my blog publishing frequency from one to two weeks in the future. Writing a post using your voice is exceptionally exhausting and time-consuming.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Hazard Pointers<\/h2>\n\n\n\n<p>The term hazard pointers goes back to Maged Michael. Hazard pointers solve the classical problem of lock-free data structures such as a lock-free stack: When can a thread safely delete a node of a data structure while other threads can use this node at the same time?<\/p>\n\n\n\n<p><br>Although a hazard pointer provides a general solution for the common problem of safe memory<br>reclamation in lock-free data structures, I want to present it from the perspective of our lock-free<br>stack.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"849\" height=\"525\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/03\/HazardPointers.png\" alt=\"\" class=\"wp-image-10685\" style=\"width:600px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/03\/HazardPointers.png 849w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/03\/HazardPointers-300x186.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/03\/HazardPointers-768x475.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/03\/HazardPointers-705x436.png 705w\" sizes=\"auto, (max-width: 849px) 100vw, 849px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>A hazard pointer is a single-writer, multi-reader pointer. All hazard pointers build a linked list and are initialized with a null pointer. When a thread uses a stack node, it puts the node\u2019s address into a hazard pointer, indicating that it uses this node and is the exclusive owner of the used hazard pointer. When the thread is done using the node, it sets the hazard pointer to a null pointer and, therefore, releases its ownership. A thread keeps a list of hazard pointers standing for the nodes the thread is using and can not be deleted. When a thread wants to delete a node, it traverses the list of all hazard pointers and checks if the node is used. If the node is not in use, it deletes it. If the node is in use, it will eventually be put on a retire list of the to-be-deleted nodes. Eventually, the node is only added to the retire list if it is not yet on the list.<\/p>\n\n\n\n<p>This is the case for our lock-free stack. The member function topAndPop has two jobs regarding memory reclamation. First, it manages the to-be-deleted node; second, it traverses the retire list of nodes and deletes them if they aren\u2019t used anymore.<\/p>\n\n\n\n<p><br>I need the following member function in a new implementation of <code>topAndPop <\/code>based on the previous description: <code>getHazardPointer <\/code>to get a reference to a hazard pointer, <code>retireList.addNode<\/code>, and <code>retireList.deleteUnusedNodes<\/code> to add a node to the retire list <code>retireList<\/code>. Additionally, <code>retireList.deleteUnusedNodes<\/code> to delete all nodes from the retire list that are no longer used. Additionally, the member function <code>retireList.deleteUnusedNode<\/code> uses the helper function <code>retireList.isInUse <\/code>to decide if a node is currently used. The member function <code>isInUse<\/code> is also handy in <code>topAndPop <\/code>decide whether the current node should be added to the retire list or directly deleted.<\/p>\n\n\n\n<p>What does this mean for my previous <code>LockFreeStack <\/code>implementation (<br><a href=\"https:\/\/www.modernescpp.com\/index.php\/a-lock-free-stack-a-complete-implementation\/\">A Lock-Free Stack: A Complete Implementation<\/a>) without memory reclamation? Let\u2019s see. The following program shows the lock-free stack implementation based on hazard pointers.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">A Hazard Pointer Implementation<\/h3>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ lockFreeStackHazardPointers.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;atomic&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;cstddef&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;future&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;stdexcept&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;thread&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold\">template<\/span> <span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span> T<span style=\"color: #555555\">&gt;<\/span>\nconcept Node <span style=\"color: #555555\">=<\/span> requires(T a) {\n    {T<span style=\"color: #555555\">::<\/span>data};\n    { <span style=\"color: #555555\">*<\/span>a.next } <span style=\"color: #555555\">-&gt;<\/span> std<span style=\"color: #555555\">::<\/span>same_as<span style=\"color: #555555\">&lt;<\/span>T<span style=\"color: #555555\">&amp;&gt;<\/span>;\n};\n\n<span style=\"color: #006699; font-weight: bold\">template<\/span> <span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span> T<span style=\"color: #555555\">&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold\">struct<\/span> MyNode {\n    T data;\n    MyNode<span style=\"color: #555555\">*<\/span> next;\n    MyNode(T d)<span style=\"color: #555555\">:<\/span> data(d), next(nullptr){ }\n};\n\nconstexpr std<span style=\"color: #555555\">::<\/span><span style=\"color: #007788; font-weight: bold\">size_t<\/span> MaxHazardPointers <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">50<\/span>;\n\n<span style=\"color: #006699; font-weight: bold\">template<\/span> <span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span> T, Node MyNode <span style=\"color: #555555\">=<\/span> MyNode<span style=\"color: #555555\">&lt;<\/span>T<span style=\"color: #555555\">&gt;&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold\">struct<\/span> HazardPointer {\n    std<span style=\"color: #555555\">::<\/span>atomic<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span><span style=\"color: #006699; font-weight: bold\">thread<\/span><span style=\"color: #555555\">::<\/span>id<span style=\"color: #555555\">&gt;<\/span> id;\n    std<span style=\"color: #555555\">::<\/span>atomic<span style=\"color: #555555\">&lt;<\/span>MyNode<span style=\"color: #555555\">*&gt;<\/span> pointer;\n};\n\n<span style=\"color: #006699; font-weight: bold\">template<\/span> <span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span> T<span style=\"color: #555555\">&gt;<\/span>\nHazardPointer<span style=\"color: #555555\">&lt;<\/span>T<span style=\"color: #555555\">&gt;<\/span> HazardPointers[MaxHazardPointers];\n\n<span style=\"color: #006699; font-weight: bold\">template<\/span> <span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span> T, Node MyNode <span style=\"color: #555555\">=<\/span> MyNode<span style=\"color: #555555\">&lt;<\/span>T<span style=\"color: #555555\">&gt;&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">HazardPointerOwner<\/span> {\n\n    HazardPointer<span style=\"color: #555555\">&lt;<\/span>T<span style=\"color: #555555\">&gt;*<\/span> hazardPointer;\n\n <span style=\"color: #9999FF\">public:<\/span>\n    HazardPointerOwner(HazardPointerOwner <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #555555\">&amp;<\/span>) <span style=\"color: #555555\">=<\/span> <span style=\"color: #006699; font-weight: bold\">delete<\/span>;\n    HazardPointerOwner <span style=\"color: #006699; font-weight: bold\">operator<\/span><span style=\"color: #555555\">=<\/span>(HazardPointerOwner <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #555555\">&amp;<\/span>) <span style=\"color: #555555\">=<\/span> <span style=\"color: #006699; font-weight: bold\">delete<\/span>;\n\n    HazardPointerOwner() <span style=\"color: #555555\">:<\/span> hazardPointer(nullptr) {\n        <span style=\"color: #006699; font-weight: bold\">for<\/span> (std<span style=\"color: #555555\">::<\/span><span style=\"color: #007788; font-weight: bold\">size_t<\/span> i <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">0<\/span>; i <span style=\"color: #555555\">&lt;<\/span> MaxHazardPointers; <span style=\"color: #555555\">++<\/span>i) {\n            std<span style=\"color: #555555\">::<\/span><span style=\"color: #006699; font-weight: bold\">thread<\/span><span style=\"color: #555555\">::<\/span>id old_id;\n            <span style=\"color: #006699; font-weight: bold\">if<\/span> (HazardPointers<span style=\"color: #555555\">&lt;<\/span>T<span style=\"color: #555555\">&gt;<\/span>[i].id.compare_exchange_strong(\n                                        old_id, std<span style=\"color: #555555\">::<\/span>this_thread<span style=\"color: #555555\">::<\/span>get_id())) {\n                hazardPointer <span style=\"color: #555555\">=<\/span> <span style=\"color: #555555\">&amp;<\/span>HazardPointers<span style=\"color: #555555\">&lt;<\/span>T<span style=\"color: #555555\">&gt;<\/span>[i];\n                <span style=\"color: #006699; font-weight: bold\">break<\/span>;\n            }\n        }\n        <span style=\"color: #006699; font-weight: bold\">if<\/span> (<span style=\"color: #555555\">!<\/span>hazardPointer) {\n            <span style=\"color: #006699; font-weight: bold\">throw<\/span> std<span style=\"color: #555555\">::<\/span>out_of_range(<span style=\"color: #CC3300\">&quot;No hazard pointers available!&quot;<\/span>);\n        }\n    }\n\n    std<span style=\"color: #555555\">::<\/span>atomic<span style=\"color: #555555\">&lt;<\/span>MyNode<span style=\"color: #555555\">*&gt;&amp;<\/span> getPointer() {\n        <span style=\"color: #006699; font-weight: bold\">return<\/span> hazardPointer<span style=\"color: #555555\">-&gt;<\/span>pointer;\n    }\n\n    <span style=\"color: #555555\">~<\/span>HazardPointerOwner() {\n        hazardPointer<span style=\"color: #555555\">-&gt;<\/span>pointer.store(nullptr);\n        hazardPointer<span style=\"color: #555555\">-&gt;<\/span>id.store(std<span style=\"color: #555555\">::<\/span><span style=\"color: #006699; font-weight: bold\">thread<\/span><span style=\"color: #555555\">::<\/span>id());\n    }\n};\n\n<span style=\"color: #006699; font-weight: bold\">template<\/span> <span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span> T, Node MyNode <span style=\"color: #555555\">=<\/span> MyNode<span style=\"color: #555555\">&lt;<\/span>T<span style=\"color: #555555\">&gt;&gt;<\/span>\nstd<span style=\"color: #555555\">::<\/span>atomic<span style=\"color: #555555\">&lt;<\/span>MyNode<span style=\"color: #555555\">*&gt;&amp;<\/span> getHazardPointer() {\n    thread_local <span style=\"color: #006699; font-weight: bold\">static<\/span> HazardPointerOwner<span style=\"color: #555555\">&lt;<\/span>T<span style=\"color: #555555\">&gt;<\/span> hazard;\n    <span style=\"color: #006699; font-weight: bold\">return<\/span> hazard.getPointer();\n}\n\n<span style=\"color: #006699; font-weight: bold\">template<\/span> <span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span> T, Node MyNode <span style=\"color: #555555\">=<\/span> MyNode<span style=\"color: #555555\">&lt;<\/span>T<span style=\"color: #555555\">&gt;&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">RetireList<\/span> {\n\n    <span style=\"color: #006699; font-weight: bold\">struct<\/span> RetiredNode {\n        MyNode<span style=\"color: #555555\">*<\/span> node;\n        RetiredNode<span style=\"color: #555555\">*<\/span> next;\n        RetiredNode(MyNode<span style=\"color: #555555\">*<\/span> p) <span style=\"color: #555555\">:<\/span> node(p), next(nullptr) { }\n        <span style=\"color: #555555\">~<\/span>RetiredNode() {\n            <span style=\"color: #006699; font-weight: bold\">delete<\/span> node;\n        }\n    };\n\n    std<span style=\"color: #555555\">::<\/span>atomic<span style=\"color: #555555\">&lt;<\/span>RetiredNode<span style=\"color: #555555\">*&gt;<\/span> RetiredNodes;\n\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #CC00FF\">addToRetiredNodes<\/span>(RetiredNode<span style=\"color: #555555\">*<\/span> retiredNode) {\n        retiredNode<span style=\"color: #555555\">-&gt;<\/span>next <span style=\"color: #555555\">=<\/span> RetiredNodes.load();\n        <span style=\"color: #006699; font-weight: bold\">while<\/span> (<span style=\"color: #555555\">!<\/span>RetiredNodes.compare_exchange_strong(retiredNode<span style=\"color: #555555\">-&gt;<\/span>next, retiredNode));\n    }\n\n <span style=\"color: #9999FF\">public:<\/span>\n\n    <span style=\"color: #007788; font-weight: bold\">bool<\/span> <span style=\"color: #CC00FF\">isInUse<\/span>(MyNode<span style=\"color: #555555\">*<\/span> node) {\n        <span style=\"color: #006699; font-weight: bold\">for<\/span> (std<span style=\"color: #555555\">::<\/span><span style=\"color: #007788; font-weight: bold\">size_t<\/span> i <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">0<\/span>; i <span style=\"color: #555555\">&lt;<\/span> MaxHazardPointers; <span style=\"color: #555555\">++<\/span>i) {\n            <span style=\"color: #006699; font-weight: bold\">if<\/span> (HazardPointers<span style=\"color: #555555\">&lt;<\/span>T<span style=\"color: #555555\">&gt;<\/span>[i].pointer.load() <span style=\"color: #555555\">==<\/span> node) <span style=\"color: #006699; font-weight: bold\">return<\/span> <span style=\"color: #336666\">true<\/span>;\n        }\n        <span style=\"color: #006699; font-weight: bold\">return<\/span> <span style=\"color: #336666\">false<\/span>;\n    }\n\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #CC00FF\">addNode<\/span>(MyNode<span style=\"color: #555555\">*<\/span> node) {\n        addToRetiredNodes(<span style=\"color: #006699; font-weight: bold\">new<\/span> RetiredNode(node));\n    }\n\n    <span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #CC00FF\">deleteUnusedNodes<\/span>() {\n        RetiredNode<span style=\"color: #555555\">*<\/span> current <span style=\"color: #555555\">=<\/span> RetiredNodes.exchange(nullptr);\n        <span style=\"color: #006699; font-weight: bold\">while<\/span> (current) {\n            RetiredNode<span style=\"color: #555555\">*<\/span> <span style=\"color: #006699; font-weight: bold\">const<\/span> next <span style=\"color: #555555\">=<\/span> current<span style=\"color: #555555\">-&gt;<\/span>next;\n            <span style=\"color: #006699; font-weight: bold\">if<\/span> (<span style=\"color: #555555\">!<\/span>isInUse(current<span style=\"color: #555555\">-&gt;<\/span>node)) <span style=\"color: #006699; font-weight: bold\">delete<\/span> current;\n            <span style=\"color: #006699; font-weight: bold\">else<\/span> addToRetiredNodes(current);\n            current <span style=\"color: #555555\">=<\/span> next;\n        }\n    }\n\n};\n\n<span style=\"color: #006699; font-weight: bold\">template<\/span><span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span> T, Node MyNode <span style=\"color: #555555\">=<\/span> MyNode<span style=\"color: #555555\">&lt;<\/span>T<span style=\"color: #555555\">&gt;&gt;<\/span>\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">LockFreeStack<\/span> {\n\n    std<span style=\"color: #555555\">::<\/span>atomic<span style=\"color: #555555\">&lt;<\/span>MyNode<span style=\"color: #555555\">*&gt;<\/span> head;\n    RetireList<span style=\"color: #555555\">&lt;<\/span>T<span style=\"color: #555555\">&gt;<\/span> retireList;\n \n <span style=\"color: #9999FF\">public:<\/span>\n    LockFreeStack() <span style=\"color: #555555\">=<\/span> <span style=\"color: #006699; font-weight: bold\">default<\/span>;\n    LockFreeStack(<span style=\"color: #006699; font-weight: bold\">const<\/span> LockFreeStack<span style=\"color: #555555\">&amp;<\/span>) <span style=\"color: #555555\">=<\/span> <span style=\"color: #006699; font-weight: bold\">delete<\/span>;\n    LockFreeStack<span style=\"color: #555555\">&amp;<\/span> <span style=\"color: #006699; font-weight: bold\">operator<\/span><span style=\"color: #555555\">=<\/span> (<span style=\"color: #006699; font-weight: bold\">const<\/span> LockFreeStack<span style=\"color: #555555\">&amp;<\/span>) <span style=\"color: #555555\">=<\/span> <span style=\"color: #006699; font-weight: bold\">delete<\/span>;\n   \n    <span style=\"color: #007788; font-weight: bold\">void<\/span> <span style=\"color: #CC00FF\">push<\/span>(T val) {\n        MyNode<span style=\"color: #555555\">*<\/span> <span style=\"color: #006699; font-weight: bold\">const<\/span> newMyNode <span style=\"color: #555555\">=<\/span> <span style=\"color: #006699; font-weight: bold\">new<\/span> MyNode(val);\n        newMyNode<span style=\"color: #555555\">-&gt;<\/span>next <span style=\"color: #555555\">=<\/span> head.load();\n        <span style=\"color: #006699; font-weight: bold\">while<\/span>( <span style=\"color: #555555\">!<\/span>head.compare_exchange_strong(newMyNode<span style=\"color: #555555\">-&gt;<\/span>next, newMyNode) );\n    }\n\n    T <span style=\"color: #CC00FF\">topAndPop<\/span>() {\n        std<span style=\"color: #555555\">::<\/span>atomic<span style=\"color: #555555\">&lt;<\/span>MyNode<span style=\"color: #555555\">*&gt;&amp;<\/span> hazardPointer <span style=\"color: #555555\">=<\/span> getHazardPointer<span style=\"color: #555555\">&lt;<\/span>T<span style=\"color: #555555\">&gt;<\/span>();\n        MyNode<span style=\"color: #555555\">*<\/span> oldHead <span style=\"color: #555555\">=<\/span> head.load();\n        <span style=\"color: #006699; font-weight: bold\">do<\/span> {\n            MyNode<span style=\"color: #555555\">*<\/span> tempMyNode; \n            <span style=\"color: #006699; font-weight: bold\">do<\/span> {\n                tempMyNode <span style=\"color: #555555\">=<\/span> oldHead;\n                hazardPointer.store(oldHead);\n                oldHead <span style=\"color: #555555\">=<\/span> head.load();\n            } <span style=\"color: #006699; font-weight: bold\">while<\/span>( oldHead <span style=\"color: #555555\">!=<\/span> tempMyNode );\n        } <span style=\"color: #006699; font-weight: bold\">while<\/span>( oldHead <span style=\"color: #555555\">&amp;&amp;<\/span> <span style=\"color: #555555\">!<\/span>head.compare_exchange_strong(oldHead, oldHead<span style=\"color: #555555\">-&gt;<\/span>next) ) ;\n        <span style=\"color: #006699; font-weight: bold\">if<\/span> ( <span style=\"color: #555555\">!<\/span>oldHead ) <span style=\"color: #006699; font-weight: bold\">throw<\/span> std<span style=\"color: #555555\">::<\/span>out_of_range(<span style=\"color: #CC3300\">&quot;The stack is empty!&quot;<\/span>);\n        hazardPointer.store(nullptr);\n        <span style=\"color: #006699; font-weight: bold\">auto<\/span> res <span style=\"color: #555555\">=<\/span> oldHead<span style=\"color: #555555\">-&gt;<\/span>data;\n        <span style=\"color: #006699; font-weight: bold\">if<\/span> ( retireList.isInUse(oldHead) ) retireList.addNode(oldHead);\n        <span style=\"color: #006699; font-weight: bold\">else<\/span> <span style=\"color: #006699; font-weight: bold\">delete<\/span> oldHead;\n        retireList.deleteUnusedNodes();\n        <span style=\"color: #006699; font-weight: bold\">return<\/span> res;\n    }\n};\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>(){\n\n    LockFreeStack<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;<\/span> lockFreeStack;\n    \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> fut <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>async([<span style=\"color: #555555\">&amp;<\/span>lockFreeStack]{ lockFreeStack.push(<span style=\"color: #FF6600\">2011<\/span>); });\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> fut1 <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>async([<span style=\"color: #555555\">&amp;<\/span>lockFreeStack]{ lockFreeStack.push(<span style=\"color: #FF6600\">2014<\/span>); });\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> fut2 <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>async([<span style=\"color: #555555\">&amp;<\/span>lockFreeStack]{ lockFreeStack.push(<span style=\"color: #FF6600\">2017<\/span>); });\n    \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> fut3 <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>async([<span style=\"color: #555555\">&amp;<\/span>lockFreeStack]{ <span style=\"color: #006699; font-weight: bold\">return<\/span> lockFreeStack.topAndPop(); });\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> fut4 <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>async([<span style=\"color: #555555\">&amp;<\/span>lockFreeStack]{ <span style=\"color: #006699; font-weight: bold\">return<\/span> lockFreeStack.topAndPop(); });\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> fut5 <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>async([<span style=\"color: #555555\">&amp;<\/span>lockFreeStack]{ <span style=\"color: #006699; font-weight: bold\">return<\/span> lockFreeStack.topAndPop(); });\n    \n    fut.get(), fut1.get(), fut2.get();\n    \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> fut3.get() <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> fut4.get() <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> fut5.get() <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n}\n<\/pre><\/div>\n\n\n\n<p>The program runs as expected.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"224\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/03\/lockFreeStackHazardPointers.png\" alt=\"\" class=\"wp-image-10687\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/03\/lockFreeStackHazardPointers.png 500w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/03\/lockFreeStackHazardPointers-300x134.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?  <\/h2>\n\n\n\n<p>I will analyse the program step by step. <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hazard Pointers solve all issues of the previous implementation: A Lock-Free Stack: A Simple Garbage Collector. From one Week to two Weeks You may have already noticed. I will change my blog publishing frequency from one to two weeks in the future. Writing a post using your voice is exceptionally exhausting and time-consuming. Hazard Pointers [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":10580,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[559],"tags":[563],"class_list":["post-10682","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c26-blog","tag-hazard-pointers"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10682","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=10682"}],"version-history":[{"count":6,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10682\/revisions"}],"predecessor-version":[{"id":10691,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10682\/revisions\/10691"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/10580"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=10682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=10682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=10682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}