Does a lock force variables to be written directly to memory instead of beëing cached like volatile does? in this question Orion Edwards states that using locks is better than using volatile, but if a public variable is accessed from within a lock, and always from that lock, does this mean that it is never cached outside of this lock statement?
private readonly object locker = new object();
private bool? _Var = null;
public bool? Var
{
get
{
lock (locker)
{
//possibly get the variable _Var in cache memory somewhere
return this._Var;
//force _Var back to memory
}
}
set
{
lock (locker)
{
//possibly get the variable _Var in cache memory somewhere
this._Var = value;
//force _Var back to memory
}
}
}