{"id":5272,"date":"2017-06-09T18:51:47","date_gmt":"2017-06-09T18:51:47","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/aba-a-is-not-the-same-as-a\/"},"modified":"2023-06-26T12:11:22","modified_gmt":"2023-06-26T12:11:22","slug":"aba-a-is-not-the-same-as-a","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/aba-a-is-not-the-same-as-a\/","title":{"rendered":"ABA &#8211; A is not the same as A"},"content":{"rendered":"<p>A common problem in concurrency is the so-called ABA problem. That means you read a value twice and each time it returns the same value A. Therefore you conclude that nothing changed in between. But you forgot the B.<\/p>\n<p><!--more--><\/p>\n<p>Let me first use a simple scenario to introduce the problem.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"An_analogy\"><\/span>An analogy<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The scenario consists of you sitting in a car and waiting that the traffic light becomes green. Green stands in our case for B, and red for A. What&#8217;s happening?<\/p>\n<ol>\n<li>You look at the traffic light and it is red (A).<\/li>\n<li>Because you are bored, you begin to check the news on your smartphone and forget the time.<\/li>\n<li>You look once more at the traffic light. Damn, is still red (A).<\/li>\n<\/ol>\n<p>Of course, the traffic light became green (B) between your two checks. Therefore, what seems to be one red phase was two.<\/p>\n<p>What does this mean for threads (processes)? Now once more formal.<\/p>\n<ol>\n<li><span class=\"mwe-math-element\">Thread 1 reads a variable var with value A. <br \/> <\/span><\/li>\n<li>Thread 1 is preempted, and thread 2 runs.<\/li>\n<li>Thread 2 changes the variable var from A to B to A.<\/li>\n<li>Thread 1 starts to execute and checks the value of variable var; because the value of variable var is the same, thread 1 continues with its work,<\/li>\n<\/ol>\n<p>Often, that is a no-brainer. You can simply ignore it.<\/p>\n<\/p>\n<h2><span class=\"ez-toc-section\" id=\"No-brainer\"><\/span>No-brainer<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Have a look at it here. The function<span style=\"font-family: courier new,courier;\"> fetch_mult<\/span> (1) multiplies a <span style=\"font-family: courier new,courier;\">std::atomic&lt;T&gt;&amp; shared<\/span> by <span style=\"font-family: courier new,courier;\">mult<\/span>.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ fetch_mult.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;atomic&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\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>\r\nT fetch_mult(std<span style=\"color: #555555;\">::<\/span>atomic<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;&amp;<\/span> shared, T mult){                          <span style=\"color: #0099ff; font-style: italic;\">\/\/ 1<\/span>\r\n  T oldValue <span style=\"color: #555555;\">=<\/span> shared.load();                                          <span style=\"color: #0099ff; font-style: italic;\">\/\/ 2<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">while<\/span> (<span style=\"color: #555555;\">!<\/span>shared.compare_exchange_strong(oldValue, oldValue <span style=\"color: #555555;\">*<\/span> mult));  <span style=\"color: #0099ff; font-style: italic;\">\/\/ 3<\/span>\r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> oldValue;\r\n}\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(){\r\n  std<span style=\"color: #555555;\">::<\/span>atomic<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">&gt;<\/span> myInt{<span style=\"color: #ff6600;\">5<\/span>};\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> myInt <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;          \r\n  fetch_mult(myInt,<span style=\"color: #ff6600;\">5<\/span>);\r\n  std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> myInt <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;         \r\n}\r\n<\/pre>\n<\/div>\n<p>The key observation is that there is a small time window between the reading of the old value <span style=\"font-family: courier new,courier;\">T oldValue = shared.load<\/span> (2) and the&nbsp;comparison with the new value (3). Therefore, another thread can kick in and change the <span style=\"font-family: courier new,courier;\">oldValue<\/span> from <span style=\"font-family: Courier New,Courier,monospace;\">oldValue<\/span> to <span style=\"font-family: Courier New,Courier,monospace;\">anotherValue<\/span> to <span style=\"font-family: Courier New,Courier,monospace;\">oldValue<\/span> back. The <span style=\"font-family: courier new,courier;\">anotherValue<\/span> is the B in ABA.<\/p>\n<p>Often it makes no difference if the first read value is in the second read operation the original value. But in a lock-free concurrent data structure, ABA may have a great impact.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"A_lock-free_data_structure\"><\/span>A lock-free data structure<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>I will not present here in detail a lock-free data structure. I will use a lock-free stack implemented as a singly linked list. The stack supports only two operations.&nbsp;<\/p>\n<ol>\n<li><span class=\"c1\">Pops the top object and returns a pointer to it.<\/span><\/li>\n<li><span class=\"c1\">Pushes the object specified&nbsp;to stack.<\/span><\/li>\n<\/ol>\n<p>Let me describe in pseudo-code the pop operation to get an idea of the ABA problem. The pop operation performs, in essence, the following steps in a loop until the operation was successful.<\/p>\n<ol>\n<li>Get the head node: <strong>head<\/strong><\/li>\n<li>Get the subsequent node: <strong>headNext<\/strong><\/li>\n<li>Make <strong>headNext<\/strong> to the new head if <strong>head<\/strong> is still the head of the stack<\/li>\n<\/ol>\n<p>Here are the first two nodes of the stack:<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">Stack: TOP <span style=\"color: #555555;\">-&gt;<\/span> head <span style=\"color: #555555;\">-&gt;<\/span> headNext -&gt; ...\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Let&#8217;s construct the ABA problem.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"ABA_in_action\"><\/span>ABA in action<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let&#8217;s start with the following stack:<\/p>\n<div style=\"background: #f0f3f3 none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\">Stack: TOP <span style=\"color: #555555;\">-&gt;<\/span> A <span style=\"color: #555555;\">-&gt;<\/span> B -&gt; C\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Thread 1 is active and wants to pop the head of stack.<\/p>\n<ul>\n<li>Thread 1 stores\n<ul>\n<li>head = A<\/li>\n<li>headNext = B<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<\/li>\n<\/ul>\n<p>Before thread 1 finishes the pop algorithm, thread 2 kicks in.<\/p>\n<ul>\n<li>Thread 2 pops A<\/li>\n<\/ul>\n<div style=\"background: #f0f3f3 none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\">    Stack: TOP <span style=\"color: #555555;\">-&gt;<\/span> <span style=\"color: #555555;\"><\/span>B -&gt; C\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>&nbsp;Thread 2 pops B and deletes B<\/li>\n<\/ul>\n<div style=\"background: #f0f3f3 none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\">    Stack: TOP<span style=\"color: #555555;\"><\/span><span style=\"color: #555555;\"> <\/span>-&gt; C\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<ul>\n<li>Thread 2 pushes A back<\/li>\n<\/ul>\n<div style=\"background: #f0f3f3 none repeat scroll 0% 0%; overflow: auto; width: auto; border-width: 0.1em 0.1em 0.1em 0.8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\">    Stack: TOP <span style=\"color: #555555;\">-&gt;<\/span> A <span style=\"color: #555555;\">-&gt;<\/span> C\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Thread 1 is rescheduled, and check if A == head. Because A == head, headNext which is B becomes the new head. But B was already deleted. Therefore, the program has undefined behavior.<\/p>\n<p>There are a few remedies for the ABA problem.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Remedy_for_ABA\"><\/span>Remedy for ABA<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The conceptual problem of ABA is relatively easy to get. A node such as B == headNext was deleted, although another node A == head was referring to it. The solution to our problem is to eliminate the premature deletion of the node. Here are a few remedies.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Tagged_state_reference\"><\/span>Tagg<span id=\"transmark\"><\/span>ed state reference<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>You can add a tag indicating how often the node has been successfully modified. The result is that compare and swap method will eventually fail, although the check returns true.&nbsp;<\/p>\n<p>The next three techniques are based on the idea of deferred reclamation.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Garbage_collection\"><\/span>Garbage collection<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Garbage collection guarantees that the variables will only be deleted if it is not needed anymore. That sounds promising but has a big drawback. Most garbage collectors are not lock-free. Therefore, you have a lock-free data structure, but the overall system is not lock-free.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Hazard_pointers\"><\/span>Hazard pointers<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>From <a href=\"https:\/\/en.wikipedia.org\/wiki\/Hazard_pointer\">Wikipedia: Hazard Pointers<\/a>:<\/p>\n<p>In a hazard-pointer system, each thread keeps a list<a href=\"https:\/\/en.wikipedia.org\/wiki\/List_%28computing%29\" class=\"mw-redirect\" title=\"List (computing)\"><\/a> of hazard pointers indicating which nodes the thread is accessing. (This &#8220;list&#8221; may be limited to only one or two elements in many systems.) Nodes on the hazard pointer list must not be modified or deallocated by any other thread. &#8230;&nbsp;When a thread wishes to remove a node, it places it on a list of nodes &#8220;to be freed later&#8221; but does not deallocate the node&#8217;s memory until no other thread&#8217;s hazard list contains the pointer. A dedicated garbage-collection thread can do this manual garbage collection (if the list &#8220;to be freed later&#8221; is shared by all the threads); alternatively, cleaning up the &#8220;to be freed&#8221; list can be done by each worker thread as part of an operation such as &#8220;pop&#8221;.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"RCU\"><\/span>RCU<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>RCU stands for <strong>R<\/strong>ead <strong>C<\/strong>opy<strong> U<\/strong>pdate and is a synchronization technique for almost read-only data structures. RCU was created by Paul McKenney and is used in the Linux Kernel since 2002.&nbsp;<\/p>\n<p>The idea is quite simple and follows the acronym. To modify data, you make a copy of the data and modify that copy. On the contrary, all readers work with the original data. You can safely replace the data structure with the copy if there is no reader.<\/p>\n<p>For more details about RCU, read the article<a href=\"https:\/\/lwn.net\/Articles\/262464\/\"> What is RCU, Fundamentally?<\/a> by Paul McKenney.<\/p>\n<p>&nbsp;<\/p>\n<p>As part of a concurrency toolkit, two proposals for upcoming C++ standards exist. The proposal <a href=\"https:\/\/www.modernescpp.com\/open-std.org\/JTC1\/SC22\/WG21\/docs\/papers\/2016\/p0233r0.pdf\">P0233r0<\/a> for hazard pointers and the proposal<a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2016\/p0461r0.pdf\"> P0461R0<\/a> for RCU.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Whats_next\"><\/span>What&#8217;s next?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>I&#8217;m not so sure. I have to go for the next big topic with the potential for at least 20 exciting posts. Let yourself be surprised.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A common problem in concurrency is the so-called ABA problem. That means you read a value twice and each time it returns the same value A. Therefore you conclude that nothing changed in between. But you forgot the B.<\/p>\n","protected":false},"author":21,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[366],"tags":[434],"class_list":["post-5272","post","type-post","status-publish","format-standard","hentry","category-multithreading","tag-atomics"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5272","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=5272"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5272\/revisions"}],"predecessor-version":[{"id":6869,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5272\/revisions\/6869"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}