{"id":1577,"date":"2019-10-21T00:15:51","date_gmt":"2019-10-20T21:15:51","guid":{"rendered":"https:\/\/blog.vtyulb.ru\/?p=1577"},"modified":"2019-10-21T00:25:27","modified_gmt":"2019-10-20T21:25:27","slug":"deprecating-volatile","status":"publish","type":"post","link":"https:\/\/blog.vtyulb.ru\/?p=1577","title":{"rendered":"Deprecating volatile"},"content":{"rendered":"<p>\u041f\u043e \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u044b\u043c \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c \u044f \u0431\u044b\u043b \u0443\u0432\u0435\u0440\u0435\u043d, \u0447\u0442\u043e static volatile int \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u043c. \u0412\u0440\u043e\u0434\u0435 \u0431\u044b \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043b\u043e\u0433\u0438\u0447\u043d\u043e &#8212; \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d\u0430, \u0447\u0442\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u0432 \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c, \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432\u0441\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e. \u041a\u0430\u043a \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u0442.<\/p>\n<p>\u041e\u0434\u043d\u043e \u0438\u0437 \u0432\u044b\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0439 \u0441 cppcon, \u0433\u0434\u0435 \u043e\u0434\u0438\u043d \u0438\u0437 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 clang \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u0435\u0442, \u0447\u0442\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 volatile:<br \/>\n<iframe loading=\"lazy\" title=\"Deprecating volatile - JF Bastien - CppCon 2019\" width=\"625\" height=\"352\" src=\"https:\/\/www.youtube.com\/embed\/KJW_DLaVXIY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>\u041f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c.<\/p>\n<p><!--more--><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">#include &lt;stdio.h&gt;\r\n#include &lt;thread&gt;\r\n#include &lt;atomic&gt;\r\n \r\nstd::atomic&lt;int&gt; acnt;\r\nstatic volatile int vcnt;\r\nconst int ub_cnt = 0;\r\nint cnt;\r\n \r\nint f(int &amp;lcnt, const int &amp;local_const_cnt)\r\n{\r\n    for(int i = 0; i &lt; 100000; i++) {\r\n        ++cnt;\r\n        ++acnt;\r\n        ++vcnt;\r\n        ++lcnt;\r\n        ++(*const_cast&lt;int*&gt;(&amp;ub_cnt)); \/\/ this will crash the program with -O0\r\n        ++(*const_cast&lt;int*&gt;(&amp;local_const_cnt));\r\n    }\r\n    return 0;\r\n}\r\n \r\nint main(void)\r\n{\r\n    int lcnt = 0;\r\n    const int local_const_cnt = 0;\r\n    \r\n    std::thread* thr&#x5B;1000];\r\n    for(int i = 0; i &lt; 1000; i++)\r\n        thr&#x5B;i] = new std::thread(f, std::ref(lcnt), std::ref(local_const_cnt));\r\n    \r\n    for (int i = 0; i &lt; 1000; i++)\r\n        thr&#x5B;i]-&gt;join();\r\n \r\n    printf(&quot;The atomic counter is %u\\n&quot;, acnt.load());\r\n    printf(&quot;The non-atomic counter is %u\\n&quot;, cnt);\r\n    printf(&quot;Volatile counter is %u\\n&quot;, vcnt);\r\n    printf(&quot;Local counter is %u\\n&quot;, lcnt);\r\n    printf(&quot;Local const counter is %u\\n&quot;, local_const_cnt);\r\n    printf(&quot;UB counter is %u\\n&quot;, ub_cnt);\r\n    \r\n    \/\/ next line will crash the program because of undefined behaviour on any optimization level\r\n    \/\/ printf(&quot;UB counter force load is %u\\n&quot;, *const_cast&lt;const volatile int*&gt;(&amp;ub_cnt));\r\n}\r\n<\/pre>\n<p>\u0414\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u043c\u0435\u0448\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b, \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0438\u0435 \u043e\u0442 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 (\u043d\u0430 -O0 ub_cnt \u044f \u0443\u0431\u0440\u0430\u043b):<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">vlad@vtyulb-thinkpad ~ % g++ main.cpp -lpthread -O0\r\nvlad@vtyulb-thinkpad ~ % .\/a.out\r\nThe atomic counter is 100000000\r\nThe non-atomic counter is 993844\r\nVolatile counter is 912750\r\nLocal counter is 1815379\r\nLocal const counter is 0\r\nUB counter is 0\r\n.\/a.out  36,80s user 0,08s system 766% cpu 4,812 total\r\nvlad@vtyulb-thinkpad ~ % .\/a.out\r\nThe atomic counter is 100000000\r\nThe non-atomic counter is 1073648\r\nVolatile counter is 938156\r\nLocal counter is 2031276\r\nLocal const counter is 0\r\nUB counter is 0\r\n.\/a.out  37,19s user 0,19s system 757% cpu 4,933 total\r\n\r\n\r\nvlad@vtyulb-thinkpad ~ % g++ main.cpp -lpthread -O1\r\nvlad@vtyulb-thinkpad ~ % .\/a.out\r\nThe atomic counter is 100000000\r\nThe non-atomic counter is 27314903\r\nVolatile counter is 1159589\r\nLocal counter is 36943434\r\nLocal const counter is 0\r\nUB counter is 0\r\n.\/a.out  36,77s user 0,14s system 764% cpu 4,829 total\r\nvlad@vtyulb-thinkpad ~ % .\/a.out\r\nThe atomic counter is 100000000\r\nThe non-atomic counter is 27600981\r\nVolatile counter is 769927\r\nLocal counter is 35498305\r\nLocal const counter is 0\r\nUB counter is 0\r\n.\/a.out  38,26s user 0,08s system 763% cpu 5,019 total\r\n\r\n\r\nvlad@vtyulb-thinkpad ~ % g++ main.cpp -lpthread -O2\r\nvlad@vtyulb-thinkpad ~ % .\/a.out\r\nThe atomic counter is 100000000\r\nThe non-atomic counter is 24814898\r\nVolatile counter is 857348\r\nLocal counter is 52322802\r\nLocal const counter is 0\r\nUB counter is 0\r\n.\/a.out  41,11s user 0,10s system 765% cpu 5,385 total\r\nvlad@vtyulb-thinkpad ~ % .\/a.out\r\nThe atomic counter is 100000000\r\nThe non-atomic counter is 24785663\r\nVolatile counter is 850257\r\nLocal counter is 51293639\r\nLocal const counter is 0\r\nUB counter is 0\r\n.\/a.out  40,76s user 0,13s system 767% cpu 5,329 total\r\n\r\n\r\nvlad@vtyulb-thinkpad ~ % g++ main.cpp -lpthread -O3\r\nvlad@vtyulb-thinkpad ~ % .\/a.out\r\nThe atomic counter is 100000000\r\nThe non-atomic counter is 25486880\r\nVolatile counter is 800002\r\nLocal counter is 55676774\r\nLocal const counter is 0\r\nUB counter is 0\r\n.\/a.out  39,96s user 0,11s system 761% cpu 5,263 total\r\nvlad@vtyulb-thinkpad ~ % .\/a.out\r\nThe atomic counter is 100000000\r\nThe non-atomic counter is 25704519\r\nVolatile counter is 686848\r\nLocal counter is 54817634\r\nLocal const counter is 0\r\nUB counter is 0\r\n.\/a.out  39,46s user 0,15s system 757% cpu 5,229 total<\/pre>\n<p>\u0412\u043e\u043e\u0431\u0449\u0435, \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b \u043f\u043e\u0447\u0442\u0438 \u0432\u0435\u0441\u044c \u0431\u043b\u043e\u0433 \u043e\u0434\u043d\u043e\u0433\u043e \u0442\u043e\u0432\u0430\u0440\u0438\u0449\u0430, \u043d\u0430\u0447\u0430\u0432 \u0441 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 <a href=http:\/\/scrutator.me\/post\/2015\/04\/05\/cpu_memory_inter_multiprocessor.aspx>http:\/\/scrutator.me\/post\/2015\/04\/05\/cpu_memory_inter_multiprocessor.aspx<\/a><br \/>\n\u042d\u0442\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0435\u0440\u0435\u0432\u0435\u0440\u043d\u0443\u043b\u043e \u043c\u043e\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043f\u0430\u043c\u044f\u0442\u0438, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0440\u0430\u043d\u044c\u0448\u0435 \u044f \u0441\u0447\u0438\u0442\u0430\u043b \u0431\u0430\u0440\u044c\u0435\u0440\u044b \u043f\u0430\u043c\u044f\u0442\u0438 \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0431\u0435\u0441\u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0444\u0438\u0433\u043d\u0435\u0439, \u0430 \u043e\u043d\u0438 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442: <a href=https:\/\/en.cppreference.com\/w\/cpp\/atomic\/atomic_thread_fence>https:\/\/en.cppreference.com\/w\/cpp\/atomic\/atomic_thread_fence<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041f\u043e \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u044b\u043c \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c \u044f \u0431\u044b\u043b \u0443\u0432\u0435\u0440\u0435\u043d, \u0447\u0442\u043e static volatile int \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u044b\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a\u043e\u043c. \u0412\u0440\u043e\u0434\u0435 \u0431\u044b \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043b\u043e\u0433\u0438\u0447\u043d\u043e &#8212; \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d\u0430, \u0447\u0442\u0435\u043d\u0438\u0435 \u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u0432 \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c, \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432\u0441\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e. \u041a\u0430\u043a \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u0442. \u041e\u0434\u043d\u043e \u0438\u0437 \u0432\u044b\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0439 \u0441 cppcon, \u0433\u0434\u0435 \u043e\u0434\u0438\u043d \u0438\u0437 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 clang \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u0435\u0442, \u0447\u0442\u043e \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[17,6,78],"class_list":["post-1577","post","type-post","status-publish","format-standard","hentry","category-59","tag-17","tag-6","tag-78"],"_links":{"self":[{"href":"https:\/\/blog.vtyulb.ru\/index.php?rest_route=\/wp\/v2\/posts\/1577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.vtyulb.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.vtyulb.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.vtyulb.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.vtyulb.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1577"}],"version-history":[{"count":10,"href":"https:\/\/blog.vtyulb.ru\/index.php?rest_route=\/wp\/v2\/posts\/1577\/revisions"}],"predecessor-version":[{"id":1587,"href":"https:\/\/blog.vtyulb.ru\/index.php?rest_route=\/wp\/v2\/posts\/1577\/revisions\/1587"}],"wp:attachment":[{"href":"https:\/\/blog.vtyulb.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vtyulb.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vtyulb.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}