I'm building a multi threaded cache in C#, which will hold a list of Car objects:
public static IList<Car> Cars {get; private set;}
I'm wondering if it's safe to change the reference in a thread without locking ?
e.g.
private static void Loop()
{
while (true)
{
Cars = GetFreshListFromServer();
Thread.Sleep(SomeInterval);
}
}
Basically it comes down to whether assigning a new reference to Cars is atomic or not I'd guess.
If it's not I'll obviously have to use a private field for my cars, and lock around getting and settings.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…