{"id":4807,"date":"2016-07-12T06:08:00","date_gmt":"2016-07-12T06:08:00","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/transivity-of-the-acquire-release-semantic\/"},"modified":"2025-02-06T16:16:05","modified_gmt":"2025-02-06T16:16:05","slug":"transivity-of-the-acquire-release-semantic","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/transivity-of-the-acquire-release-semantic\/","title":{"rendered":"Transitivity of the Acquire-Release Semantic"},"content":{"rendered":"<p>A release operation synchronizes with an acquire operation on the same atomic variable and establishes, in addition, ordering constraints. These are the components to synchronize threads in a performant way in case they act on the same atomic. But how can that work, if two threads share no atomic variable? We want no sequential consistency because that is too heavy. We want the light acquire-release semantic.<\/p>\n<p><!--more--><\/p>\n<p>The answer to the riddle is easy. Because of the transitivity of the acquire-release semantic, threads can be synchronized, which act independently of each other.<\/p>\n<h2>Transitivity<\/h2>\n<p>In the following example, thread <span style=\"font-family: courier new,courier;\">t2<\/span>, with its work package <span style=\"font-family: courier new,courier;\">deliveryBoy <\/span>is the glue between the two independent threads <span style=\"font-family: courier new,courier;\">t1<\/span> and <span style=\"font-family: courier new,courier;\">t3<\/span>.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ transitivity.cpp<\/span>\n\n<span style=\"color: #0000ff;\">#include &lt;atomic&gt;<\/span>\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #0000ff;\">#include &lt;thread&gt;<\/span>\n<span style=\"color: #0000ff;\">#include &lt;vector&gt;<\/span>\n\nstd::vector&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; mySharedWork;\nstd::atomic&lt;<span style=\"color: #2b91af;\">bool<\/span>&gt; dataProduced(false);\nstd::atomic&lt;<span style=\"color: #2b91af;\">bool<\/span>&gt; dataConsumed(false);\n\n<span style=\"color: #2b91af;\">void<\/span> dataProducer(){\n    mySharedWork={1,0,3};\n    dataProduced.store(true, std::memory_order_release);\n}\n\n<span style=\"color: #2b91af;\">void<\/span> deliveryBoy(){\n    <span style=\"color: #0000ff;\">while<\/span>( !dataProduced.load(std::memory_order_acquire) );\n    dataConsumed.store(true,std::memory_order_release);\n}\n\n<span style=\"color: #2b91af;\">void<\/span> dataConsumer(){\n    <span style=\"color: #0000ff;\">while<\/span>( !dataConsumed.load(std::memory_order_acquire) );\n    mySharedWork[1]= 2;\n}\n\n<span style=\"color: #2b91af;\">int<\/span> main(){\n    \n  std::cout &lt;&lt; std::endl;\n\n  std::<span style=\"color: #0000ff;\">thread<\/span> t1(dataConsumer);\n  std::<span style=\"color: #0000ff;\">thread<\/span> t2(deliveryBoy);\n  std::<span style=\"color: #0000ff;\">thread<\/span> t3(dataProducer);\n\n  t1.join();\n  t2.join();\n  t3.join();\n  \n  <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #0000ff;\">auto<\/span> v: mySharedWork){\n      std::cout &lt;&lt; v &lt;&lt; <span style=\"color: #a31515;\">\" \"<\/span>;\n  }\n      \n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"\\n\\n\"<\/span>;\n  \n}\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>The output of the program is deterministic. <span style=\"font-family: courier new,courier;\">mySharedWork<\/span> will have the values 1,2, and 3.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4805\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/trans.png\" alt=\"trans\" width=\"399\" height=\"179\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/trans.png 399w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/trans-300x135.png 300w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><\/p>\n<p>Why is the program deterministic? There are two critical observations:<\/p>\n<ol>\n<li>Thread<span style=\"font-family: courier new,courier;\"> t2<\/span> waits in line 18 until thread <span style=\"font-family: courier new,courier;\">t1<\/span> has set <span style=\"font-family: courier new,courier;\">dataProduced<\/span> on <span style=\"font-family: courier new,courier;\">true<\/span> (line 14).<\/li>\n<li>Thread<span style=\"font-family: courier new,courier;\"> t3<\/span> waits in line 23 until thread <span style=\"font-family: courier new,courier;\">t2<\/span> has set <span style=\"font-family: courier new,courier;\">dataConsumed<\/span> on <span style=\"font-family: courier new,courier;\">true<\/span> (line 19).<\/li>\n<\/ol>\n<p>The rest is easier explained with a picture.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4806\" style=\"margin: 15px;\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/transitivity.PNG\" alt=\"transitivity\" width=\"800\" height=\"396\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/transitivity.PNG 1011w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/transitivity-300x148.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/transitivity-768x380.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/p>\n<p>The critical parts of the picture are the arrows.<\/p>\n<ul>\n<li>The <span style=\"color: #3366ff;\">blue<\/span> arrows are the\u00a0<em>sequenced-before<\/em> relations. All operations in one thread will be executed in source code order.<\/li>\n<li>The <span style=\"color: #ff0000;\">red<\/span> arrows are the <em>synchronize-with<\/em> relations. The reason is the acquire-release semantic of the atomic operations on the same atomic. So the synchronization between the threads takes place.<\/li>\n<li>As well<em> sequenced-before<\/em> as <em>synchronizes-with<\/em> establishes a <em>happens-before<\/em> relation.<\/li>\n<\/ul>\n<p>The rest is pretty simple. The chronological order of the instructions (<em>happens-before<\/em>) corresponds to the direction of the arrows from top to bottom. So, we have the guarantee that <span style=\"font-family: courier new,courier;\">mySharedWork[1] == 2<\/span> will be executed last.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>That was a concise post. But that is my plan to keep the parts easy to digest. That will change with the <a href=\"https:\/\/www.modernescpp.com\/index.php\/memory-order-consume\">next post<\/a> because I will write about the legendary<span style=\"font-family: courier new,courier;\"> std::memory_order_consume<\/span> memory order.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A release operation synchronizes with an acquire operation on the same atomic variable and establishes, in addition, ordering constraints. These are the components to synchronize threads in a performant way in case they act on the same atomic. But how can that work, if two threads share no atomic variable? We want no sequential consistency [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":4805,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[368],"tags":[],"class_list":["post-4807","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-multithreading-memory-model"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4807","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=4807"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4807\/revisions"}],"predecessor-version":[{"id":10635,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4807\/revisions\/10635"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4805"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=4807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=4807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=4807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}