【发布时间】:2014-06-19 10:47:48
【问题描述】:
锁是否强制变量直接写入内存,而不是像 volatile 那样被缓存?在this question Orion Edwards 中指出使用锁比使用 volatile 更好,但是如果从锁内访问公共变量,并且总是从该锁访问,这是否意味着它永远不会在该锁语句之外缓存?
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
}
}
}
【问题讨论】: