{"id":4837,"date":"2016-08-03T07:11:18","date_gmt":"2016-08-03T07:11:18","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/ongoing-optimization\/"},"modified":"2023-06-26T12:48:05","modified_gmt":"2023-06-26T12:48:05","slug":"ongoing-optimization","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/ongoing-optimization\/","title":{"rendered":"Ongoing Optimization"},"content":{"rendered":"<p>Now it&#8217;s time to put the theory into practice. The job is relatively easy. A small program should undergo ongoing optimization.<\/p>\n<p><!--more--><\/p>\n<h2>The program<\/h2>\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<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ ongoingOptimization.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;thread&gt;<\/span>\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> x= 0;\r\n<span style=\"color: #2b91af;\">int<\/span> y= 0;\r\n\r\n<span style=\"color: #2b91af;\">void<\/span> writing(){\r\n  x= 2000;\r\n  y= 11;\r\n}\r\n\r\n<span style=\"color: #2b91af;\">void<\/span> reading(){ \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"y: \"<\/span> &lt;&lt; y &lt;&lt; <span style=\"color: #a31515;\">\" \"<\/span>;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"x: \"<\/span> &lt;&lt; x &lt;&lt; std::endl;\r\n}\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> thread1(writing);\r\n  std::<span style=\"color: #0000ff;\">thread<\/span> thread2(reading);\r\n  thread1.join();\r\n  thread2.join();\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The program is quite simple. It consists of two threads <span style=\"font-family: courier new,courier;\">thread1<\/span> and <span style=\"font-family: courier new,courier;\">thread2.thread1 <\/span>writes the values x and y. <span style=\"font-family: courier new,courier;\">thread 2 <\/span>reads the values x and y in the opposite direction.&nbsp; The execution of the program is not so thrilling either.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4835\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/08\/ongoingOptimization.png\" alt=\"ongoingOptimization\" style=\"margin: 15px;\" width=\"377\" height=\"314\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/08\/ongoingOptimization.png 377w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/08\/ongoingOptimization-300x250.png 300w\" sizes=\"auto, (max-width: 377px) 100vw, 377px\" \/><\/p>\n<p>But you see, even this simple program has different results. So I finally came to my key idea.<\/p>\n<\/p>\n<p>In the process of the ongoing optimization of the program, I have two questions.<\/p>\n<ol>\n<li>Is the program well-defined? Precisely, is there a <a href=\"https:\/\/www.modernescpp.com\/index.php\/threads-sharing-data\">data race<\/a>?<\/li>\n<li>Which values for x and y are possible?<\/li>\n<\/ol>\n<p>The first question is often very challenging to answer. So I will reason in the first step about the program and verify in the second step my reasoning with <a href=\"https:\/\/www.modernescpp.com\/index.php\/cppmem-an-overview\">CppMem<\/a>.&nbsp; If I have answered the first question, the second answer can&nbsp;easily be derived.&nbsp;I will provide the possible values in a table.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4836\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/08\/schabloneEng.png\" alt=\"schabloneEng\" width=\"288\" height=\"225\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>But still, one question isn&#8217;t answered yet. What do I mean by ongoing optimization? I mean, by ongoing optimization, that I try to improve&nbsp;the program by weakening the C++ memory model. So in my journey, I discuss the following stations.<\/p>\n<ul>\n<li>Non-atomic variables<\/li>\n<li>Locks<\/li>\n<li>Atomics with sequential consistency<\/li>\n<li>Atomics with acquire-release semantic<\/li>\n<li>Atomics with relaxed semantic<\/li>\n<li>Volatile variables<\/li>\n<\/ul>\n<h2>What&#8217;s next?<\/h2>\n<p>Of course, this was a very short post. But this post should provide you only the context of my ongoing optimization. In case you are bored, reason about the small program and answer the two question.<\/p>\n<ol>\n<li>Is the program well-defined?<\/li>\n<li>Which values for x and y are possible?<\/li>\n<\/ol>\n<p>Still bored? Then reason about the weakening of the program&#8217;s memory model and the consequences. In the <a href=\"https:\/\/www.modernescpp.com\/index.php\/ongoing-optimization-2\">next post<\/a>, I will analyze the unsynchronized access with the help of&nbsp;<a href=\"https:\/\/www.modernescpp.com\/index.php\/cppmem-an-overview\">CppMem<\/a>. Stay tuned.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<a href=\"https:\/\/www.modernescpp.com\/index.php\/source-code-repository\"><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now it&#8217;s time to put the theory into practice. The job is relatively easy. A small program should undergo ongoing optimization.<\/p>\n","protected":false},"author":21,"featured_media":4835,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[369],"tags":[486,521],"class_list":["post-4837","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-multithreading-application","tag-cppmem","tag-ongoing-optimization"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4837","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=4837"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4837\/revisions"}],"predecessor-version":[{"id":6959,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4837\/revisions\/6959"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4835"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=4837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=4837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=4837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}