Recently, I was refactoring some of my C # code, and I found several double-validation checks. I did not know that it was a bad practice, and I really want to get rid of it.
The problem is that I have a class that needs to be lazily initialized and often accessible by multiple threads. I also do not want to move initialization to a static initializer, because I plan to use a weak reference to keep the initialized object in memory too long. However, if necessary, I want to “animate” the object, ensuring that this happens in a thread-safe manner.
I was wondering if you use ReaderWriterLockSlim in C # and enter UpgradeableReadLock before the first check, and then enter a write lock for initialization if necessary, this would be an acceptable solution. Here is what I mean:
public class LazyInitialized
{
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
private volatile WeakReference _valueReference = new WeakReference(null);
public MyType Value
{
get
{
MyType value = _valueReference.Target as MyType;
_lock.EnterUpgradeableReadLock();
try
{
if (!_valueReference.IsAlive)
{
_lock.EnterWriteLock();
try
{
if (!_valueReference.IsAlive)
{
Thread.MemoryBarrier();
_valueReference = new WeakReference(value = InitializeMyType());
}
}
finally
{
_lock.ExitWriteLock();
}
}
}
finally
{
_lock.ExitUpgradeableReadLock();
}
return value;
}
}
private MyType InitializeMyType()
{
}
}
, , . , , , , , , . , , , .
, , , _valueReference . .