{"id":19084,"date":"2025-08-22T10:26:37","date_gmt":"2025-08-22T10:26:37","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=19084"},"modified":"2025-08-22T10:36:43","modified_gmt":"2025-08-22T10:36:43","slug":"usecase-for-compare_exchange-stdatomic","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=19084","title":{"rendered":"Usecase for compare_exchange (std::atomic)"},"content":{"rendered":"<p>Let&#8217;s assume you want to implement a function to update the maximum value or a variable.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\ntemplate&lt;class T&gt;\r\nvoid update_maximum(std::atomic&lt;T&gt;&amp; max_value, const T&amp; value)\r\n{\r\n    if (max_value.load() &lt; value)\r\n    {\r\n        max_value.store(value);\r\n    }\r\n}\r\n<\/pre>\n<p>Issue: To prevent a lost update for those two atomic steps, you would need a lock.<br \/>\nSolution: std::atomic provides a mechanism to do even those two steps atomically.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\ntemplate&lt;class T&gt;\r\nvoid update_maximum(std::atomic&lt;T&gt;&amp; max_value, const T&amp; value)\r\n{\r\n    T current = max_value.load();\r\n    while (current &lt; value &amp;&amp;\r\n       !max_value.compare_exchange_weak(current, value))\r\n    {}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s assume you want to implement a function to update the maximum value or a variable. Issue: To prevent a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[63],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/19084"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19084"}],"version-history":[{"count":16,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/19084\/revisions"}],"predecessor-version":[{"id":19100,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/19084\/revisions\/19100"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19084"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}