I have class Lazy
which lazily evaluates an expression:
public sealed class Lazy<T>
{
Func<T> getValue;
T value;
public Lazy(Func<T> f)
{
getValue = () =>
{
lock (getValue)
{
value = f();
getValue = () => value;
}
return value;
};
}
public T Force()
{
return getValue();
}
}
Basically, I'm trying to avoid the overhead of locking objects after they've been evaluated, so I replace getValue
with another function on invocation.
It apparently works in my testing, but I have no way of knowing if it'll blow up in production.
Is my class threadsafe? If not, what can be done to guarantee thread safety?