我读了一本书:
“记忆障碍是昂贵的,就像
昂贵的原子compareAndSet()
指令.”
谢谢!
解决方法
“Memory barriers are expensive,about as expensive as an atomic compareAndSet() instruction.”
这是真的
例如.在x86上,多处理器系统上的正确CAS具有锁定前缀.
锁前缀导致完整的内存障碍:
“…locked operations serialize all outstanding load and store operations (that is,wait for them to complete).” …”Locked operations are atomic with respect to all other memory operations and all externally visible events. Only instruction fetch and page table accesses can pass locked instructions. Locked instructions can be used to synchronize data written by one processor and read by another processor.” – 07001,Chapter 8.1.2.
实际上,存储器屏障实现为虚拟LOCK OR或LOCK,并且在x86 / x64上的the .NET和the JAVA JIT中实现.
在x86上,CAS导致完整的内存障碍.
在PPC上,它是不同的. LL/SC对 – lwarx
& stwcx
– 可用于将内存操作数加载到寄存器中,如果目标位置没有其他存储,则将其写回,或者如果存在则重试整个循环. LL / SC可以中断.
它也不意味着一个自动的全屏障.
性能特征和行为在不同架构上可能会有很大差异.
但再次 – weak LL/SC不是CAS.