I have the following code:
public class Info
{
public int Data { get; set; }
}
public class Updater
{
private static readonly object lockObject = new object();
private Info myInfo= new Info();
public Info MyInfo
{
get
{
lock (lockObject)
{
return myInfo;
}
}
}
public void UpdateInfo()
{
lock (lockObject)
{
myInfo.Data = ReadFromExternalDevice();
}
}
}
I have one instance of Updater
which is accessed from two separate threads.
Updater updater = new Updater();
Thread #1 periodically calls UpdateInfo()
.
updater.UpdateInfo();
Thread #2 periodically reads from the Data
property of the Info
property.
int latestData = updater.MyInfo.Data;
Is the above code in anywhere close to being thread-safe?