Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Volatile Class

.NET Framework 4.5

Contains methods for performing volatile memory operations.

System.Object
  System.Threading.Volatile

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public static class Volatile

  NameDescription
Public methodStatic memberSupported in .NET for Windows Store appsRead(Boolean)Reads the value of the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
Public methodStatic memberSupported in .NET for Windows Store appsRead(Byte)Reads the value of the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
Public methodStatic memberSupported in .NET for Windows Store appsRead(Double)Reads the value of the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
Public methodStatic memberSupported in .NET for Windows Store appsRead(Int16)Reads the value of the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
Public methodStatic memberSupported in .NET for Windows Store appsRead(Int32)Reads the value of the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
Public methodStatic memberSupported in .NET for Windows Store appsRead(Int64)Reads the value of the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
Public methodStatic memberSupported in .NET for Windows Store appsRead(IntPtr)Reads the value of the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
Public methodStatic memberSupported in .NET for Windows Store appsRead(SByte)Reads the value of the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
Public methodStatic memberSupported in .NET for Windows Store appsRead(Single)Reads the value of the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
Public methodStatic memberSupported in .NET for Windows Store appsRead(UInt16)Reads the value of the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
Public methodStatic memberSupported in .NET for Windows Store appsRead(UInt32)Reads the value of the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
Public methodStatic memberSupported in .NET for Windows Store appsRead(UInt64)Reads the value of the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
Public methodStatic memberSupported in .NET for Windows Store appsRead(UIntPtr)Reads the value of the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
Public methodStatic memberSupported in .NET for Windows Store appsRead<T>(T)Reads the object reference from the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
Public methodStatic memberSupported in .NET for Windows Store appsWrite(Boolean, Boolean)Writes the specified value to the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.
Public methodStatic memberSupported in .NET for Windows Store appsWrite(Byte, Byte)Writes the specified value to the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.
Public methodStatic memberSupported in .NET for Windows Store appsWrite(Double, Double)Writes the specified value to the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.
Public methodStatic memberSupported in .NET for Windows Store appsWrite(Int16, Int16)Writes the specified value to the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.
Public methodStatic memberSupported in .NET for Windows Store appsWrite(Int32, Int32)Writes the specified value to the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.
Public methodStatic memberSupported in .NET for Windows Store appsWrite(Int64, Int64)Writes the specified value to the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a memory operation appears before this method in the code, the processor cannot move it after this method.
Public methodStatic memberSupported in .NET for Windows Store appsWrite(IntPtr, IntPtr)Writes the specified value to the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.
Public methodStatic memberSupported in .NET for Windows Store appsWrite(SByte, SByte)Writes the specified value to the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.
Public methodStatic memberSupported in .NET for Windows Store appsWrite(Single, Single)Writes the specified value to the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.
Public methodStatic memberSupported in .NET for Windows Store appsWrite(UInt16, UInt16)Writes the specified value to the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.
Public methodStatic memberSupported in .NET for Windows Store appsWrite(UInt32, UInt32)Writes the specified value to the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.
Public methodStatic memberSupported in .NET for Windows Store appsWrite(UInt64, UInt64)Writes the specified value to the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.
Public methodStatic memberSupported in .NET for Windows Store appsWrite(UIntPtr, UIntPtr)Writes the specified value to the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.
Public methodStatic memberSupported in .NET for Windows Store appsWrite<T>(T, T)Writes the specified object reference to the specified field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.
Top

On a multiprocessor system, a volatile write operation ensures that a value written to a memory location is immediately visible to all processors. A volatile read operation obtains the very latest value written to a memory location by any processor. These operations might require flushing processor caches, which can affect performance.

NoteNote

On a uniprocessor system, volatile reads and writes ensure that a value is read or written to memory and not cached (for example, in a processor register). Thus, you can use these operations to synchronize access to a field that can be updated by another thread or by hardware.

Volatile memory operations are for special cases of synchronization, where normal locking is not an acceptable alternative. Under normal circumstances, the C# lock statement, the Visual Basic SyncLock statement, and the Monitor class provide the easiest and least error-prone way of synchronizing access to data, and the Lazy<T> class provides a simple way to write lazy initialization code without directly using double-checked locking.

The Volatile.Read and Volatile.Write methods enable functionality that is not supported in languages. For example:

  • Some languages, such as Visual Basic, do not recognize the concept of volatile memory operations. The Volatile class provides that functionality in such languages.

    NoteNote

    Calling one of these methods affects only a single memory access. To provide effective synchronization for a field, all access to the field must use Volatile.Read and Volatile.Write.

  • In C#, using the volatile modifier on a field guarantees that every access to that field uses the Volatile.Read and Volatile.Write methods, but the volatile modifier cannot be applied to array elements. The Volatile.Read and Volatile.Write methods can be used on array elements.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft