如上图,C++方面并无区别。然而汇编方面并非如此。
public class UnsafeDemo {
public static void main(String[] args) {
Unsafe unSafe = UnsafeUtil.getUnSafe();
assert unSafe != null;
// allocateMemory(unSafe);
// copyMemory(unSafe);
AtomicInteger atomicInteger = new AtomicInteger();
testDiffOfSetAndLazySet(atomicInteger);
}
/**
* set方法和lazySet方法的区别
*/
public static void testDiffOfSetAndLazySet(AtomicInteger atomicInteger) {
for (int i = 0; i < 10000; i++) {
//set方法等价putXXXVolatile
atomicInteger.set(i);
}
for (int i = 0; i < 10000; i++) {
// 等价putOrderedXXX
atomicInteger.lazySet(i);
}
}
}
但在其他CPU架构下,storestore屏障应该会显现出来。
同样的C++代码,怎么会生成不同的汇编码的呢?
C++内联。如下图。根据不同的intrinsic id生成不同的指令集。
inline_unsafe_access(!is_native_ptr, is_store, T_OBJECT, is_volatile)如下
如果是多核处理器,那就加入lock指令模板