I have a simple class like :
public class XXX
{
double val1;
double val2;
double delta;
public void SetValues(double v1, double v2)
{
val1 = v1;
val2 = v2;
delta = val1 - val2;
}
public double Val1 { get { return val1; } }
public double Val2 { get { return val2; } }
public double Delta { get { return delta; } }
}
, one thread for setting values and multiple threads that read the values. So one can use lock()
to make all read and writes uninterrupted.
But I am aware that synchronization is never achived, there is always a chance that Val1 - Val2
may not be equal to Delta
which I do not care so much. My concern is more about obtaining the stable values via getters. However lock()
is expensive for this situation because mostly the readers will work.
The next best thing that come to my mind is using Interlocked.Exchange()
public void SetValues(double v1, double v2)
{
Interlocked.Exchange(ref val1, v1);
Interlocked.Exchange(ref val2, v2);
Interlocked.Exchange(ref delta, v1 - v2);
}
public double Val1 {
get
{
double val = 0;
Interlocked.Exchange(ref val, val1);
return val;
}
}
But the code seems pretty dum to me. I don't know.
So does lock()
makes sense? Should I use the Interlocked.Exchange()
for performance gain? Or what else can I do?