noc*_*ura 4 .net multithreading
例:
Thread a: Interlocked.Increment(ref x);
Thread b: int currentValue = x;
Run Code Online (Sandbox Code Playgroud)
假设线程b在线程a之后执行,则线程b中的"currentValue"是否保证是递增值?或者,线程b是否需要执行Thread.VolatileRead(ref x)?
从技术上讲,这取决于CPU运行的CPU,但对于任何常见的CPU,答案都是肯定的,Interlocked.Increment保证了缓存一致性.它充当 MESI要求的记忆屏障.
CPU可以在其高速缓存中具有无效的行,但是在它还不知道该行无效的情况下 - 失效队列包含尚未对其起作用的失效.(失效队列位于缓存的另一侧"; CPU无法扫描它,因为它可以存储缓冲区").结果,需要存储器障碍.
http://en.wikipedia.org/wiki/MESI_protocol(x86)
MOESI(用于AMD64芯片)非常相似:
http://en.wikipedia.org/wiki/MOESI_protocol(AMD64)