I have a question regarding the Java Memory Model (JMM), particularly in the context of x86 architecture, which I find quite intriguing. One of the most confusing and often debated topics is the volatile
modifier.
I’ve heard a lot of misconceptions suggesting that volatile
effectively forbids the use of cached values for fields marked with this modifier. Some even claim it prohibits the use of registers. However, as far as I understand, these are oversimplified notions. I’ve never encountered any instructions that explicitly forbid using caches or registers for storing such fields. I’m not even sure if such behavior is technically possible.
So, my question is directed at experts in x86 architecture: What actually happens under the hood? What semantics does the volatile
modifier guarantee? From what I’ve seen, it seems to implement a full memory barrier using the LOCK
prefix combined with the add 0
instruction.
Let’s settle this debate once and for all.
P.S. I’m really tired of hearing false claims from my fellow programmers about volatile
. They keep repeating the same story about cache usage, and I strongly feel they are terribly mistaken!
I have researched the Java Memory Model (JMM) and the use of the volatile
modifier. I expected to find clear explanations on how volatile
works in the context of x86 architecture, specifically regarding its impact on caching and register usage. However, I encountered conflicting information and misconceptions. I am seeking clarification from experts to understand the true semantics and behavior of volatile
on x86 systems.