I need to get this code from .NET 4.5+ working on .NET 4.0. The generic Volatile.Read
isn't available in that version. I'm trying to use the Thread.VolatileRead
to get this working. I need to know if this outright a bad idea and if not, what I should test for?
See what I replaced in the commented line.
internal static TValue EnsureSingletonInitialized<TValue, TArg>(ref TValue value,
TArg arg, Func<TArg, TValue> initialize)
where TValue : class
{
//TValue currentValue = Volatile.Read(ref value);
object o = value;
TValue currentValue = (TValue)Thread.VolatileRead(ref o);
if (null != currentValue)
return currentValue;
TValue candidateValue = initialize(arg);
return Interlocked.CompareExchange(ref value, candidateValue, null) ?? candidateValue;
}