Volatile Class
TOC
Collapse the table of content
Expand the table of content

Volatile Class

.NET Framework (current version)
 

Contains methods for performing volatile memory operations.

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

System.Object
  System.Threading.Volatile

public static class Volatile

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticRead(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.

System_CAPS_pubmethodSystem_CAPS_staticRead(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.

System_CAPS_pubmethodSystem_CAPS_staticRead(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.

System_CAPS_pubmethodSystem_CAPS_staticRead(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.

System_CAPS_pubmethodSystem_CAPS_staticRead(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.

System_CAPS_pubmethodSystem_CAPS_staticRead(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.

System_CAPS_pubmethodSystem_CAPS_staticRead(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.

System_CAPS_pubmethodSystem_CAPS_staticRead(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.

System_CAPS_pubmethodSystem_CAPS_staticRead(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.

System_CAPS_pubmethodSystem_CAPS_staticRead(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.

System_CAPS_pubmethodSystem_CAPS_staticRead(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.

System_CAPS_pubmethodSystem_CAPS_staticRead(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.

System_CAPS_pubmethodSystem_CAPS_staticRead(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.

System_CAPS_pubmethodSystem_CAPS_staticRead<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.

System_CAPS_pubmethodSystem_CAPS_staticWrite(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.

System_CAPS_pubmethodSystem_CAPS_staticWrite(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.

System_CAPS_pubmethodSystem_CAPS_staticWrite(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.

System_CAPS_pubmethodSystem_CAPS_staticWrite(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.

System_CAPS_pubmethodSystem_CAPS_staticWrite(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.

System_CAPS_pubmethodSystem_CAPS_staticWrite(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.

System_CAPS_pubmethodSystem_CAPS_staticWrite(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.

System_CAPS_pubmethodSystem_CAPS_staticWrite(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.

System_CAPS_pubmethodSystem_CAPS_staticWrite(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.

System_CAPS_pubmethodSystem_CAPS_staticWrite(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.

System_CAPS_pubmethodSystem_CAPS_staticWrite(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.

System_CAPS_pubmethodSystem_CAPS_staticWrite(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.

System_CAPS_pubmethodSystem_CAPS_staticWrite(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.

System_CAPS_pubmethodSystem_CAPS_staticWrite<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.

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.

System_CAPS_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 methodsenable 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.

    System_CAPS_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.

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1

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

Return to top
Show:
© 2016 Microsoft