This topic has not yet been rated - Rate this topic

Thread.VolatileRead Method (Object%)

Updated: May 2011

Reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)
public static Object VolatileRead(
	ref Object address
)

Parameters

address
Type: System.Object%
The field to be read.

Return Value

Type: System.Object
The latest value written to the field by any processor.

VolatileRead and VolatileWrite are for special cases of synchronization. Under normal circumstances, the C# lock statement, the Visual Basic SyncLock statement, and the Monitor class provide easier alternatives.

On a multiprocessor system, VolatileRead obtains the very latest value written to a memory location by any processor. This might require flushing processor caches.

Even on a uniprocessor system, VolatileRead and VolatileWrite ensure that a value is read or written to memory, and not cached (for example, in a processor register). Thus, you can use them to synchronize access to a field that can be updated by another thread, or by hardware.

Calling this method affects only a single memory access. To provide effective synchronization for a field, all access to the field must use VolatileRead or VolatileWrite.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Date

History

Reason

May 2011

Removed erroneous statement about C#.

Content bug fix.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Other Reference Types
This overload seems most vexing: it can take only a ref to an object, not any other reference type. If it were generic, or the parameter were not 'ref', you could use it rather more.

And you may want this. Normally you can declare any reference-typed field to be volatile, and the compiler takes care of you. However, you can't do this to events. The default event add and remove handlers are lock-free and threadsafe, but to fire an event you must extract the delegate from it, and this has no special protection (though the CLR 2.0 memory model affords some extra protection).

Turns out there's less than meets the eye here. The implementation just copies the parameter into a local variable, then performs a memory barrier. You can perform a memory barrier yourself with Thread.MemoryBarrier(); so the generic version of VolatileRead() is

static T VolatileRead<T>(ref T obj) where T : class { T copy=obj; Thread.MemoryBarrier(); return copy; }

If you do this yourself, remember that the barrier follows the actual read, which is a little counter-intuitive. The idea is not to get the 'most recent' value of the field, but rather to ensure that no data can be extracted from the object it before we do read the reference to it (and therefore, perhaps before it was even initialized).

It seems intuitively impossible for the program to read the fields of the object before we read the reference to it, and indeed the CLR won't do it. But there are architectures where it is possible in theory: you just need a stale cache of the object's fields somewhere.