After reading about LazyInitializer
that it's :
It offers another mode of initialization that has multiple threads race to initialize.
here is a sample :
Expensive _expensive;
public Expensive Expensive
{
get // Implement double-checked locking
{
LazyInitializer.EnsureInitialized (ref _expensive,() => new Expensive());
return _expensive;
}
}
Question #1
looking at :
why #A says its implements double-checking locking ? it is just a get proeprty ?
Question #2
Does #B (lambda expression) is thread safe ?
Question #3
So i searched about this "race-to-initialize" thing by looking at a sample:
volatile Expensive _expensive;
public Expensive Expensive
{
get
{
if (_expensive == null)
{
var instance = new Expensive();
Interlocked.CompareExchange (ref _expensive, instance, null);
}
return _expensive;
}
}
and then i thought about : isnt race to initialize is thread safe ?
e/g/ if 2 threads get into :
the expensive object will be created twice !
So again , 3 question
1)why #A says its implements double-checking locking ? it is just a get proeprty ?
2)Does #B (lambda expression) is thread safe ?
3)isnt race to initialize is thread safe