c# - cached variables using lock and volatile -
c# - cached variables using lock and volatile - does lock forcefulness variables written straight memory instead of beëing cached volatile does? in this question orion edwards states using locks improve using volatile, if public variable accessed within lock, , lock, mean never cached outside of lock statement? private readonly object locker = new object(); private bool? _var = null; public bool? var { { lock (locker) { //possibly variable _var in cache memory somewhere homecoming this._var; //force _var memory } } set { lock (locker) { //possibly variable _var in cache memory somewhere this._var = value; //force _var memory } } } a lock introduces acquire-fence before first instruction, , release-fence after lastly instruction. the acquire-fence prevents instructions within lock beingness moved backwards in time , ab