See Also: Volatile Members
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.
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 System.Threading.Monitor class provide the easiest and least error-prone way of synchronizing access to data, and the Lazy`1 class provides a simple way to write lazy initialization code without directly using double-checked locking.
Some languages, such as Visual Basic, do not recognize the concept of volatile memory operations. The System.Threading.Volatile class provides that functionality in such languages.
In C#, using the volatile modifier on a field guarantees that every access to that field uses the Volatile.Read(Byte@) and Volatile.Write(Byte@, byte) methods, but the volatile modifier cannot be applied to array elements. The Volatile.Read(Byte@) and Volatile.Write(Byte@, byte) methods can be used on array elements.