{"id":10893,"date":"2011-04-19T07:00:00","date_gmt":"2011-04-19T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/04\/19\/visual-studio-2005-gives-you-acquire-and-release-semantics-for-free-on-volatile-memory-access\/"},"modified":"2011-04-19T07:00:00","modified_gmt":"2011-04-19T07:00:00","slug":"visual-studio-2005-gives-you-acquire-and-release-semantics-for-free-on-volatile-memory-access","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110419-00\/?p=10893","title":{"rendered":"Visual Studio 2005 gives you acquire and release semantics for free on volatile memory access"},"content":{"rendered":"<p>\nIf you are using\nVisual Studio 2005 or later, then you don&#8217;t need the weird\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/04\/12\/10152296.aspx\">\n<code>Interlocked&shy;Read&shy;Acquire<\/code><\/a>\nfunction\nbecause\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/12a04hfd(v=VS.80).aspx\">\nVisual Studio 2005 and later automatically impose acquire semantics\non reads from volatile locations<\/a>.\nIt also imposes release semantics on writes to volatile locations.\nIn other words, you can replace the old\n<code>Interlocked&shy;Read&shy;Acquire<\/code>\nfunction with the following:\n<\/p>\n<pre>\n#if <a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/b0084kay(v=VS.80).aspx\">_MSC_VER &gt;= 1400<\/a>\nLONG InterlockedReadAcquire(__in volatile LONG *pl)\n{\n    return *pl; \/\/ Acquire imposed by volatility\n}\n#endif\n<\/pre>\n<p>\nThis is a good thing because it expresses your intentions more\nclearly to the compiler.\nThe old method that overloaded\n<code>Interlocked&shy;Compare&shy;Exchange&shy;Acquire<\/code>\nforced the compiler to perform the actual compare-and-exchange\neven though we really didn&#8217;t care about the operation; we just\nwanted the side effect of the Acquire semantics.\nOn some architectures, this forces the cache line dirty\n<a HREF=\"http:\/\/www.bing.com\/search?q=&quot;To+simplify+the+interface+to+the+processor's+bus,+the+destination+operand+receives+a+write+cycle+without+regard+to+the+result+of+the+comparison&quot;\">\neven if the comparison fails<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are using Visual Studio 2005 or later, then you don&#8217;t need the weird Interlocked&shy;Read&shy;Acquire function because Visual Studio 2005 and later automatically impose acquire semantics on reads from volatile locations. It also imposes release semantics on writes to volatile locations. In other words, you can replace the old Interlocked&shy;Read&shy;Acquire function with the following: [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-10893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>If you are using Visual Studio 2005 or later, then you don&#8217;t need the weird Interlocked&shy;Read&shy;Acquire function because Visual Studio 2005 and later automatically impose acquire semantics on reads from volatile locations. It also imposes release semantics on writes to volatile locations. In other words, you can replace the old Interlocked&shy;Read&shy;Acquire function with the following: [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=10893"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10893\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=10893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=10893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=10893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}