1.java volatile的底层实现为啥是lock memory呢 是不是可以用原子指令实现呢
jmm定义store-load的程序顺序,volatile需要在前后添加load-store,store-load来保证共享变量在多线程间的可见性。
volatile实现时使用的lock mem,应该不只是锁住内存、更新cache,而是会更新register;
如果使用原子指令修改,其他线程未必能感知到对共享变量的修改。但本质上可行
https://wiki.sei.cmu.edu/confluence/display/c/CON03-C.+Ensure+visibility+when+accessing+shared+variables
2.mesi等缓存一致性与volatile 、cache coherency shared variable visibility in multiple thread
缓存一致性保证了多core间缓存的一致性,保证的是最终一致性?对寄存器无用。
volatile规定的是多线程间的顺序保证。
缓存一致性用词用错了,coherence!http://homepage.cs.uiowa.edu/~ghosh/4-20-06.pdf
https://stackoverflow.com/questions/3242577/whats-the-point-of-cache-coherency
https://stackoverflow.com/questions/60292095/mesi-protocol-stdatomic-does-it-ensure-all-writes-are-immediately-visible
有个volatile的例子,bool值,当加入sout后就能生效了,很可能是作用到了register刷新,因为缓存是一致的,只有寄存器可能是缓存的。但是解释不通的地方是,cpu应该会有进程切换,这时寄存器应该就切换了啊
如果能看到cpu里面的真实情况就好了!!!
c++也得用类似的volatile来声明变量在多线程中的可见性:https://stackoverflow.com/questions/41448644/cache-coherency-of-a-shared-boolean-value-c11
mesi and related
https://stackoverflow.com/questions/35511686/how-does-cache-coherence-work-in-multi-core-and-multi-processor-architecture
c++ volatile https://stackoverflow.com/questions/4557979/when-to-use-volatile-with-multi-threading
volatile 触发re-read 可能直接从cache read 而 cache值 已经生效了https://stackoverflow.com/questions/558848/can-i-force-cache-coherency-on-a-multicore-x86-cpu
https://stackoverflow.com/questions/21129018/java-volatile-and-cache-coherence
http://www.cs.cornell.edu/courses/cs3410/2011sp/lecture/27-atomic-w.pdf
https://stackoverflow.com/questions/14758088/how-are-atomic-operations-implemented-at-a-hardware-level
https://stackoverflow.com/questions/8819095/concurrency-atomic-and-volatile-in-c11-memory-model