{"id":4804,"date":"2016-07-04T09:30:23","date_gmt":"2016-07-04T09:30:23","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/acquire-release-semantic\/"},"modified":"2023-06-26T12:51:16","modified_gmt":"2023-06-26T12:51:16","slug":"acquire-release-semantic","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/acquire-release-semantic\/","title":{"rendered":"Acquire-Release Semantic"},"content":{"rendered":"<p>With the acquire-release semantics, the memory model gets very thrilling. Because now, we do not have to reason about the synchronization of threads, we have to reason about the synchronization of the same atomic in different threads.<\/p>\n<p><!--more--><\/p>\n<h2>Synchronization and Ordering Constraints<\/h2>\n<p>The acquire-release semantic is based on one key idea. A release operation synchronizes with an acquire operation on the same atomic and establishes, in addition, an ordering constraint. So, all read and write operations can not be moved before an acquire operation; all read and write operations can not be moved behind a release operation. But what is an acquire or release operation? Reading an atomic variable with <span style=\"font-family: courier new,courier;\">load<\/span> or <span style=\"font-family: courier new,courier;\">test_and_set<\/span> is an acquire operation. But in addition, the acquiring of a lock. Of course, the opposite is also true. The releasing of a lock is a release operation\u2014accordingly, a <span style=\"font-family: courier new,courier;\">store<\/span> or <span style=\"font-family: courier new,courier;\">clear<\/span> operation on an atomic variable.<\/p>\n<\/p>\n<p>It&#8217;s worth more reasoning on the few last sentences from a different perspective. The lock of a mutex is an acquire operation; the unlock of a mutex is a release operation. That implies that an operation on a variable can not be moved outside of a <a href=\"https:\/\/www.modernescpp.com\/index.php\/threads-sharing-data\">critical section<\/a>. That hold for both directions. On the other side, a variable can be moved inside of a critical section. Because the variable moves from the not protected to the protected area. Now with a little delay, the picture.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4803\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/criticalRegion.png\" alt=\"criticalRegion\" width=\"322\" height=\"318\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/criticalRegion.png 322w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/criticalRegion-300x296.png 300w\" sizes=\"auto, (max-width: 322px) 100vw, 322px\" \/><\/p>\n<p>Did II not promise it? The acquire-release semantics helps understand the lock better and unlock operation of a mutex. The same reasoning will&nbsp;hold for the <a href=\"https:\/\/www.modernescpp.com\/index.php\/thread-creation\">starting of a thread<\/a> or the <span style=\"font-family: courier new,courier;\">join<\/span>-call on a thread. Both are release operations. But that story continues with the <span style=\"font-family: courier new,courier;\">wait<\/span> and<span style=\"font-family: courier new,courier;\"> notify_one<\/span>-call on a <a href=\"https:\/\/www.modernescpp.com\/index.php\/condition-variables\">condition variable<\/a>. <span style=\"font-family: courier new,courier;\">wait<\/span> is this case is the acquire, <span style=\"font-family: courier new,courier;\">notify_one<\/span> the release operation. But what is about <span style=\"font-family: courier new,courier;\">notify_all<\/span>? Of course, you can already guess it. That is a release operation.<\/p>\n<p>Because of the theory, I can write the spinlock from the post <a href=\"https:\/\/www.modernescpp.com\/index.php\/the-atomic-flag\">The atomic flag<\/a> is more efficient because the synchronization takes place on the atomic variable <span style=\"font-family: courier new,courier;\">flag.<\/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\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ spinlockAcquireRelease.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;atomic&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;thread&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Spinlock<\/span>{\r\n  std::atomic_flag flag;\r\npublic:\r\n  Spinlock(): flag(ATOMIC_FLAG_INIT) {}\r\n\r\n  <span style=\"color: #2b91af;\">void<\/span> lock(){\r\n    <span style=\"color: #0000ff;\">while<\/span>(flag.test_and_set(std::memory_order_acquire) );\r\n  }\r\n\r\n  <span style=\"color: #2b91af;\">void<\/span> unlock(){\r\n    flag.clear(std::memory_order_release);\r\n  }\r\n};\r\n\r\nSpinlock spin;\r\n\r\n<span style=\"color: #2b91af;\">void<\/span> workOnResource(){\r\n  spin.lock();\r\n  <span style=\"color: #008000;\">\/\/ shared resource<\/span>\r\n  spin.unlock();\r\n}\r\n\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> t(workOnResource);\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> t2(workOnResource);\r\n\r\n  t.join();\r\n  t2.join();\r\n\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The flag.clear-call in line 16 is a release, and the flag.test_and_set-call in line 12 is an acquire-operation. And &#8211; but that is boring &#8211; the acquire synchronizes with the release operation. So the heavyweight synchronization with sequential consistency <span style=\"font-family: courier new,courier;\">(std::memory_order_seq_cst<\/span>) of two threads is replaced with the lightweight and more performant acquire-release semantic (<span style=\"font-family: courier new,courier;\">std::memory_order_acquire <\/span>and<span style=\"font-family: courier new,courier;\"> std::memory_order_release<\/span>). The behavior is unchanged.<\/p>\n<p>In case more than two threads use the spinlock, the acquire semantics of the lock method is not sufficient. Now the lock method is an acquire-release operation. So the memory model in line 12 has to be changed to<span style=\"font-family: courier new,courier;\"> std::memory_order_acq_rel. <\/span>But that is still cheaper than the default: <span style=\"font-family: courier new,courier;\">std::memory_order_seq_cst.<\/span><span style=\"font-family: courier new,courier;\"> <\/span><\/p>\n<p>&nbsp;<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>Our journey through the memory model continues in the <a href=\"https:\/\/www.modernescpp.com\/index.php\/transivity-of-the-acquire-release-semantic\">next post<\/a> with the transitivity of the acquire-release semantic. So you can synchronize threads that share no atomic.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the acquire-release semantics, the memory model gets very thrilling. Because now, we do not have to reason about the synchronization of threads, we have to reason about the synchronization of the same atomic in different threads.<\/p>\n","protected":false},"author":21,"featured_media":4803,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[368],"tags":[505,434,519],"class_list":["post-4804","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-multithreading-memory-model","tag-acquire-release-semantic","tag-atomics","tag-sequential-consistency"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4804","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=4804"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4804\/revisions"}],"predecessor-version":[{"id":6966,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4804\/revisions\/6966"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4803"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=4804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=4804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=4804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}