Volatile (computer programming): Difference between revisions

Content deleted Content added
Lede: increase in scope for article
Rescuing 2 sources and tagging 0 as dead.) #IABot (v2.0.9.3) (Whoop whoop pull up - 12955
Line 246:
==In C#==
 
In [[C Sharp (programming language)|C#]], <code>volatile</code> ensures that code accessing the field is not subject to some thread-unsafe optimizations that may be performed by the compiler, the CLR, or by hardware. When a field is marked <code>volatile</code>, the compiler is instructed to generate a "memory barrier" or "fence" around it, which prevents instruction reordering or caching tied to the field. When reading a <code>volatile</code> field, the compiler generates an ''acquire-fence'', which prevents other reads and writes to the field, including those in other threads, from being moved ''before'' the fence. When writing to a <code>volatile</code> field, the compiler generates a ''release-fence''; this fence prevents other reads and writes to the field from being moved ''after'' the fence.<ref name="Albahari">{{cite web |last1=Albahari |first1=Joseph |title=Part 4: Advanced Threading |url=http://www.albahari.com/threading/part4.aspx#_Nonblocking_Synchronization |website=Threading in C# |publisher=O'Reilly Media |access-date=9 December 2019 |archive-url=https://web.archive.org/web/20191212032535/http://www.albahari.infocom/threading/threadingpart4.pdfaspx#_Nonblocking_Synchronization |archive-date=2712 AprilDecember 2019 2011|url-status=livebot: unknown }}</ref>
 
Only the following types can be marked <code>volatile</code>: all reference types, <code>Single</code>, <code>Boolean</code>, <code>Byte</code>, <code>SByte</code>, <code>Int16</code>, <code>UInt16</code>, <code>Int32</code>, <code>UInt32</code>, <code>Char</code>, and all enumerated types with an underlying type of <code>Byte</code>, <code>SByte</code>, <code>Int16</code>, <code>UInt16</code>, <code>Int32</code>, or <code>UInt32</code>.<ref>{{cite book |last1=Richter |first1=Jeffrey |title=CLR Via C# |url=https://archive.org/details/clrviac00rich_000 |url-access=limited |publisher=Microsoft Press |date=February 11, 2010 |pages=[https://archive.org/details/clrviac00rich_000/page/n200 183] |chapter=Chapter 7: Constants and Fields |isbn=978-0-7356-2704-8}}</ref> (This excludes value [[struct]]s, as well as the primitive types <code>Double</code>, <code>Int64</code>, <code>UInt64</code> and <code>Decimal</code>.)
Line 259:
 
==In Fortran==
<code>VOLATILE</code> is part of the [[Fortran#Fortran 2003|Fortran 2003]] standard,<ref>{{cite web |url=http://docs.cray.com/books/S-3692-51/html-S-3692-51/zfixedn3c8sk4c.html|title=VOLATILE Attribute and Statement|publisher=Cray|access-date=2016-04-22|archive-date=2018-01-23|archive-url=https://web.archive.org/web/20180123165050/http://docs.cray.com/books/S-3692-51/html-S-3692-51/zfixedn3c8sk4c.html|url-status=dead}}</ref> although earlier version supported it as an extension. Making all variables <code>volatile</code> in a function is also useful finding [[aliasing (computing)|aliasing]] related bugs.
<syntaxhighlight lang="fortran">
integer, volatile :: i ! When not defined volatile the following two lines of code are identical