I have read plenty of discussions and examples of how to make a property threadsafe. There is one on the page for this threadsafe wrapper class
The example given is this:
internal class MyThreadSafeCass
{
// *** Lock ***
private object PropertyLock = new object();
// *** Property ***
private int m_Property = 0;
// *** Thread-safe access to Property using locking ***
internal int Property
{
get
{
lock (PropertyLock)
{
return m_Property;
}
}
set
{
lock (PropertyLock)
{
m_Property = value;
}
}
}
}
It is clear what's happening here, and what the locks are doing, but I'm struggling to see why it is required. Why is the following not threadsafe? What can go wrong?
internal class MyThreadSafeCass
{
// *** Property ***
private int m_Property = 0;
// *** Thread-safe access to Property using locking ***
internal int Property
{
get
{
return m_Property;
}
set
{
m_Property = value;
}
}
}