{"id":4409,"date":"2014-03-30T10:42:17","date_gmt":"2014-03-30T10:42:17","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/what-is-the-purpose-of-volatile-keyword-in-c-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:42:27","modified_gmt":"2022-08-30T15:42:27","slug":"what-is-the-purpose-of-volatile-keyword-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/what-is-the-purpose-of-volatile-keyword-in-c-collection-of-common-programming-errors\/","title":{"rendered":"What is the purpose of &#39;volatile&#39; keyword in C#-Collection of common programming errors"},"content":{"rendered":"<p>I refer you to section 10.5.3 of the specification, which states:<\/p>\n<blockquote>\n<p>For non-volatile fields, optimization techniques that reorder instructions can lead to unexpected and unpredictable results in multi-threaded programs that access fields without synchronization such as that provided by the lock-statement (\u00a78.12). These optimizations can be performed by the compiler, by the run-time system, or by hardware. For volatile fields, such reordering optimizations are restricted:<\/p>\n<p>A read of a volatile field is called a volatile read. A volatile read has &#8220;acquire semantics&#8221;; that is, it is guaranteed to occur prior to any references to memory that occur after it in the instruction sequence.<\/p>\n<p>A write of a volatile field is called a volatile write. A volatile write has &#8220;release semantics&#8221;; that is, it is guaranteed to happen after any memory references prior to the write instruction in the instruction sequence.<\/p>\n<p>These restrictions ensure that all threads will observe volatile writes performed by any other thread in the order in which they were performed. A conforming implementation is not required to provide a single total ordering of volatile writes as seen from all threads of execution.<\/p>\n<\/blockquote>\n<p>Read that <em>extremely carefully<\/em> if you have any intention of ever making a volatile field. If you do not <em>completely and thoroughly<\/em> understand all the implications of volatile semantics then do not attempt to use them. It is usually far better to use a lock, which automatically gives you sufficient memory barriers to ensure the necessary acquire and release semantics. Remember, locks are only really expensive when they are contended.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I refer you to section 10.5.3 of the specification, which states: For non-volatile fields, optimization techniques that reorder instructions can lead to unexpected and unpredictable results in multi-threaded programs that access fields without synchronization such as that provided by the lock-statement (\u00a78.12). These optimizations can be performed by the compiler, by the run-time system, or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[],"class_list":["post-4409","post","type-post","status-publish","format-standard","hentry","category-semantic","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4409"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4409\/revisions"}],"predecessor-version":[{"id":8787,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4409\/revisions\/8787"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}