{"id":4831,"date":"2016-07-27T07:18:19","date_gmt":"2016-07-27T07:18:19","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/relaxed-semantic\/"},"modified":"2023-10-21T19:55:54","modified_gmt":"2023-10-21T19:55:54","slug":"relaxed-semantic","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/relaxed-semantic\/","title":{"rendered":"Relaxed Semantics"},"content":{"rendered":"<p>The relaxed semantics is the end of the scale. The relaxed semantic is the weakest of all memory models and guarantees that the operations on atomic variables are atomic.<\/p>\n<p><!--more--><\/p>\n<h3>No synchronization and ordering constraints<\/h3>\n<p>That&#8217;s relatively easy. If there are no rules, we can not break them. But that&#8217;s too easy. The program should have well-defined behavior. That means, in this case: No <a href=\"https:\/\/www.modernescpp.com\/index.php\/threads-sharing-data\">race condition<\/a>. To guarantee this, you typically use synchronization and ordering constraints of stronger memory models to control operations with relaxed semantics. How does this work? A thread can see the effects of another thread in arbitrary order. So, you must only be sure that there are points in your program in which all operations on all threads get synchronized.<\/p>\n<p>A typical example of an atomic operation, in which the sequence of operations doesn&#8217;t matter, is a counter. The key of a counter is not, in which order the different threads increment the counter. The key of the counter is that all increments are atomic and all threads are done at the end. Have a look at the example.<\/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<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ relaxed.cpp<\/span>\n\n<span style=\"color: #0000ff;\">#include &lt;vector&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;atomic&gt;<\/span>\n \nstd::atomic&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; cnt = {0};\n \n<span style=\"color: #2b91af;\">void<\/span> f()\n{\n    <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #2b91af;\">int<\/span> n = 0; n &lt; 1000; ++n) {\n        cnt.fetch_add(1, std::memory_order_relaxed);\n    }\n}\n \n<span style=\"color: #2b91af;\">int<\/span> main()\n{\n    std::vector&lt;std::<span style=\"color: #0000ff;\">thread<\/span>&gt; v;\n    <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #2b91af;\">int<\/span> n = 0; n &lt; 10; ++n) {\n        v.emplace_back(f);\n    }\n    <span style=\"color: #0000ff;\">for<\/span> (<span style=\"color: #0000ff;\">auto<\/span>&amp; t : v) {\n        t.join();\n    }\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Final counter value is \"<\/span> &lt;&lt; cnt &lt;&lt; <span style=\"color: #a31515;\">'\\n'<\/span>;\n}\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The three most exciting lines are 13, 24, and 26.<\/p>\n<p>In line 13, the atomic number <span style=\"font-family: courier new,courier;\">cnt<\/span> is incremented with relaxed semantics. So, we have the guarantee that the operation is atomic. The <span style=\"font-family: courier new,courier;\">fetch_add<\/span> operation established an ordering on <span style=\"font-family: courier new,courier;\">cnt.<\/span> The function <span style=\"font-family: courier new,courier;\">f<\/span> (lines 10 &#8211; 15) is the work package of the threads. Each thread gets its work package in line 21.&nbsp;<\/p>\n<p>Thread creation is one synchronization point. The other synchronization point is the <span style=\"font-family: courier new,courier;\">t.join() <\/span>call in line 24.&nbsp;<\/p>\n<p>The creator thread synchronizes with all its children in line 24. It waits with the&nbsp; <span style=\"font-family: courier new,courier;\">t.join()<\/span> call until all its children are done. <span style=\"font-family: courier new,courier;\">t.join()<\/span> is the reason that the results of the atomic operations are published. To say it more formally: <span style=\"font-family: courier new,courier;\">t.join()<\/span> is a release operation.<\/p>\n<p>In the end, there is a <em>happen-before<\/em> relation between the increment operation in line 13 and the reading of the counter <span style=\"font-family: courier new,courier;\">cnt<\/span> in line 26.<\/p>\n<p>The result is, that the program always returns 10000. Boring? No, calming!<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4830\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/relaxed.png\" alt=\"relaxed\" width=\"527\" height=\"470\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/relaxed.png 527w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/07\/relaxed-300x268.png 300w\" sizes=\"auto, (max-width: 527px) 100vw, 527px\" \/><\/p>\n<p>A typical example of an atomic counter using the relaxed semantic is the reference counter of <span style=\"font-family: courier new,courier;\">std::shared_ptr<\/span>. That will only hold for the increment operation. The key for incrementing the reference counter is that the operation is atomic. The order of the increment operations does not matter. That will not hold for the decrementation of the reference counter. These operations need an acquire-release semantic with the destructor.<\/p>\n<p>I want to explicitly say thanks to Anthony Williams, author of the well-known book C++ Concurrency in Action. He gave me very valuable tips for this post. Anthony writes his blog to concurrency in modern C++: <a href=\"https:\/\/www.justsoftwaresolutions.co.uk\/blog\/\">https:\/\/www.justsoftwaresolutions.co.uk\/blog\/.<\/a><\/p>\n<h2>Business before pleasure<\/h2>\n<p>Business before pleasure. That&#8217;s my simple motto for the next posts. So, I will use the theory about <a href=\"https:\/\/www.modernescpp.com\/index.php\/atomics\">atomics<\/a> and the <a href=\"https:\/\/www.modernescpp.com\/index.php\/c-memory-model\">memory model<\/a> in practice.<\/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<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #2b91af;\">int<\/span> x= 0;\n<span style=\"color: #2b91af;\">int<\/span> y= 0;\n\n<span style=\"color: #2b91af;\">void<\/span> writing(){\n  x= 2000;\n  y= 11;\n}\n\n<span style=\"color: #2b91af;\">void<\/span> reading(){ \n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"y: \"<\/span> &lt;&lt; y &lt;&lt; <span style=\"color: #a31515;\">\" \"<\/span>;\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"x: \"<\/span> &lt;&lt; x &lt;&lt; std::endl;\n}\n\n<span style=\"color: #2b91af;\">int<\/span> main(){\n  std::<span style=\"color: #0000ff;\">thread<\/span> thread1(writing);\n  std:.<span style=\"color: #0000ff;\">thread<\/span> thread2(reading);\n  thread1.join();\n  thread2.join();\n};<span id=\"transmark\"><\/span>\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The program looks quite simple. But it has undefined behavior. Why? I will answer the question in the following post. But that is only the first step. I want to optimize the program. If you want to play with atomics and the memory model, it&#8217;s always a good idea to have a static code analyzer at your disposal. So, in the next post, I will introduce the invaluable, precious tool <a href=\"http:\/\/svr-pes20-cppmem.cl.cam.ac.uk\/cppmem\/\">CppMem.<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The relaxed semantics is the end of the scale. The relaxed semantic is the weakest of all memory models and guarantees that the operations on atomic variables are atomic.<\/p>\n","protected":false},"author":21,"featured_media":4830,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[368],"tags":[434,504],"class_list":["post-4831","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-multithreading-memory-model","tag-atomics","tag-relaxed-semantics"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4831","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=4831"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4831\/revisions"}],"predecessor-version":[{"id":8531,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4831\/revisions\/8531"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4830"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=4831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=4831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=4831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}