{"id":10651,"date":"2025-02-17T11:47:15","date_gmt":"2025-02-17T11:47:15","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=10651"},"modified":"2025-07-03T14:35:42","modified_gmt":"2025-07-03T14:35:42","slug":"a-lock-free-stack-atomic-smart-pointer","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/a-lock-free-stack-atomic-smart-pointer\/","title":{"rendered":"A Lock-Free Stack: Atomic Smart Pointer"},"content":{"rendered":"\n<p>The easiest way to solve this memory leak issue from the last post is to use a<code> std::shared_ptr<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter 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\">Atomic Smart Pointer<\/h2>\n\n\n\n<p>There are two ways to apply atomic operations on a <code>std::shared_ptr<\/code>: In C++11, you can use the free atomic functions on<code> std::shared_ptr.<\/code> With C++20, you can use atomic smart pointers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">C++11<\/h3>\n\n\n\n<p>Using atomic operations on <code>std::shared_ptr<\/code> is tedious and error-prone. You can easily forget the atomic operations, and all bets are open. The following example shows a lock-free stack based on <code>std::shared_ptr<\/code>.<\/p>\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\">\/\/ lockFreeStackWithSharedPtr.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;atomic&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;memory&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>\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">LockFreeStack<\/span> {\n <span style=\"color: #9999FF\">public:<\/span>\n    <span style=\"color: #006699; font-weight: bold\">struct<\/span> Node {\n        T data;\n        std<span style=\"color: #555555\">::<\/span>shared_ptr<span style=\"color: #555555\">&lt;<\/span>Node<span style=\"color: #555555\">&gt;<\/span> next;\n    };\n    std<span style=\"color: #555555\">::<\/span>shared_ptr<span style=\"color: #555555\">&lt;<\/span>Node<span style=\"color: #555555\">&gt;<\/span> head;\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        <span style=\"color: #006699; font-weight: bold\">auto<\/span> newNode <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>make_shared<span style=\"color: #555555\">&lt;<\/span>Node<span style=\"color: #555555\">&gt;<\/span>();\n        newNode<span style=\"color: #555555\">-&gt;<\/span>data <span style=\"color: #555555\">=<\/span> val;\n        newNode<span style=\"color: #555555\">-&gt;<\/span>next <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>atomic_load(<span style=\"color: #555555\">&amp;<\/span>head);                                       <span style=\"color: #0099FF; font-style: italic\">\/\/ 1<\/span>\n        <span style=\"color: #006699; font-weight: bold\">while<\/span>( <span style=\"color: #555555\">!<\/span>std<span style=\"color: #555555\">::<\/span>atomic_compare_exchange_strong(<span style=\"color: #555555\">&amp;<\/span>head, <span style=\"color: #555555\">&amp;<\/span>newNode<span style=\"color: #555555\">-&gt;<\/span>next, newNode) ); <span style=\"color: #0099FF; font-style: italic\">\/\/ 2<\/span>\n    }\n\n    T <span style=\"color: #CC00FF\">topAndPop<\/span>() {\n        <span style=\"color: #006699; font-weight: bold\">auto<\/span> oldHead <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>atomic_load(<span style=\"color: #555555\">&amp;<\/span>head);                                       <span style=\"color: #0099FF; font-style: italic\">\/\/ 3<\/span>\n        <span style=\"color: #006699; font-weight: bold\">while<\/span>( oldHead <span style=\"color: #555555\">&amp;&amp;<\/span> <span style=\"color: #555555\">!<\/span>std<span style=\"color: #555555\">::<\/span>atomic_compare_exchange_strong(<span style=\"color: #555555\">&amp;<\/span>head, <span style=\"color: #555555\">&amp;<\/span>oldHead, std<span style=\"color: #555555\">::<\/span>atomic_load(<span style=\"color: #555555\">&amp;<\/span>oldHead<span style=\"color: #555555\">-&gt;<\/span>next)) ) {     <span style=\"color: #0099FF; font-style: italic\">\/\/ 4<\/span>\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        }\n        <span style=\"color: #006699; font-weight: bold\">return<\/span> oldHead<span style=\"color: #555555\">-&gt;<\/span>data;\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>This lock-free stack implementation is quite similar to the previous one without memory reclamation. The main difference is that the nodes are of type <code>std::shared_ptr<\/code>. All operations <code>std::shared_ptr <\/code>are done atomically by using the free atomic operations <code>std::load<\/code> (lines 1 and 4), and <code>std::atomic_compare_exchange_strong<\/code> (lines 2 and 3). The free atomics operations require a pointer. I want to emphasize explicitly, the read operation of the next node in <code>oldHead-&gt;next<\/code> (line 4) must be atomic because <code>oldHead-&gt;next<\/code> can be used by other threads. <\/p>\n\n\n\n<p>Finally, here is the output of the program.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"470\" height=\"215\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/02\/lockFreeStackWithSharedPtr.png\" alt=\"\" class=\"wp-image-10656\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/02\/lockFreeStackWithSharedPtr.png 470w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/02\/lockFreeStackWithSharedPtr-300x137.png 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/figure>\n\n\n\n<p>Let\u2019s jump nine years into the future and use C++20.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">C++20<br><\/h3>\n\n\n\n<p>C++20 supports partial specializations of<code> std::atomic<\/code> for <code>std::shared_ptr <\/code>and <code>std::weak_ptr<\/code>. The following implementation puts the nodes of the lock-free stack into a <code>std::atomic&lt;std::shared_ptr&lt;Node&gt;<\/code>&gt;.<\/p>\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\">\/\/ lockFreeStackWithAtomicSharedPtr.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;atomic&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;memory&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>\n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">LockFreeStack<\/span> {\n <span style=\"color: #9999FF\">private:<\/span>\n    <span style=\"color: #006699; font-weight: bold\">struct<\/span> Node {\n        T data;\n        std<span style=\"color: #555555\">::<\/span>shared_ptr<span style=\"color: #555555\">&lt;<\/span>Node<span style=\"color: #555555\">&gt;<\/span> next;\n    };\n    std<span style=\"color: #555555\">::<\/span>atomic<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>shared_ptr<span style=\"color: #555555\">&lt;<\/span>Node<span style=\"color: #555555\">&gt;&gt;<\/span> head;           <span style=\"color: #0099FF; font-style: italic\">\/\/ 1<\/span>\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) {                              <span style=\"color: #0099FF; font-style: italic\">\/\/ 2<\/span>\n        <span style=\"color: #006699; font-weight: bold\">auto<\/span> newNode <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>make_shared<span style=\"color: #555555\">&lt;<\/span>Node<span style=\"color: #555555\">&gt;<\/span>();\n        newNode<span style=\"color: #555555\">-&gt;<\/span>data <span style=\"color: #555555\">=<\/span> val;\n        newNode<span style=\"color: #555555\">-&gt;<\/span>next <span style=\"color: #555555\">=<\/span> head;\n        <span style=\"color: #006699; font-weight: bold\">while<\/span>( <span style=\"color: #555555\">!<\/span>head.compare_exchange_strong(newNode<span style=\"color: #555555\">-&gt;<\/span>next, newNode) );\n    }\n\n    T <span style=\"color: #CC00FF\">topAndPop<\/span>() {\n        <span style=\"color: #006699; font-weight: bold\">auto<\/span> oldHead <span style=\"color: #555555\">=<\/span> head.load();\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        }\n        <span style=\"color: #006699; font-weight: bold\">return<\/span> oldHead<span style=\"color: #555555\">-&gt;<\/span>data;\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 main difference between the previous and this implementation is that the <code>Node<\/code> is embedded into a <code>std::atomic&lt;std::shared_ptr&lt;Node><\/code>> (line 1). Consequentially, the member function <code>push <\/code>(line 2) creates a<code> std::shared_ptr&lt;Node><\/code> and the call <code>head.load()<\/code> in the member function <code>topAndPop <\/code>returns a <code>std::atomic&lt;std::shared_ptr&lt;Node><\/code>>.<\/p>\n\n\n\n<p>Here is the output of the program.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"488\" height=\"154\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/02\/lockFreeStackWithAtomicSharedPtr.png\" alt=\"\" class=\"wp-image-10659\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/02\/lockFreeStackWithAtomicSharedPtr.png 488w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2025\/02\/lockFreeStackWithAtomicSharedPtr-300x95.png 300w\" sizes=\"auto, (max-width: 488px) 100vw, 488px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><code>std::atomic&lt;std::shared_ptr<\/code>&gt; is not Lock-Free <\/h4>\n\n\n\n<p>Honestly, I cheated in the previous programs using atomic operations on a <code>std::shared_ptr <\/code>and a <code>std::atomic<\/code>. Those atomic operations on a<code> std::shared_ptr<\/code> are currently not lock-free. Additionally, an implementation of <code>std::atomic<\/code> can use a locking mechanism to support all partial and full specializations of<code> std::atomic<\/code>. <\/p>\n\n\n\n<p>The call <code>atom.lock_free()<\/code> on a <code>std::atomic&lt;std::shared_ptr&lt;Node&gt;<\/code>&gt;  returns <code>false<\/code>.<\/p>\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\">\/\/ atomicSmartPointer.cpp<\/span>\n \n<span style=\"color: #009999\">#include &lt;atomic&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;memory&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>\n<span style=\"color: #006699; font-weight: bold\">struct<\/span> Node {\n    T data;\n    std<span style=\"color: #555555\">::<\/span>shared_ptr<span style=\"color: #555555\">&lt;<\/span>Node<span style=\"color: #555555\">&gt;<\/span> next;   \n};\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {    \n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>boolalpha;\n\n    std<span style=\"color: #555555\">::<\/span>atomic<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>shared_ptr<span style=\"color: #555555\">&lt;<\/span>Node<span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;&gt;&gt;<\/span> node;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;node.is_lock_free(): &quot;<\/span>  <span style=\"color: #555555\">&lt;&lt;<\/span> node.is_lock_free() <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n}\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p>So, we are back to square one and need to worry about memory management in my next post.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The easiest way to solve this memory leak issue from the last post is to use a std::shared_ptr. Atomic Smart Pointer There are two ways to apply atomic operations on a std::shared_ptr: In C++11, you can use the free atomic functions on std::shared_ptr. With C++20, you can use atomic smart pointers. C++11 Using atomic operations [&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,399],"class_list":["post-10651","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c26-blog","tag-hazard-pointers","tag-smart-pointers"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10651","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=10651"}],"version-history":[{"count":10,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10651\/revisions"}],"predecessor-version":[{"id":10663,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/10651\/revisions\/10663"}],"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=10651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=10651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=10651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}