Processors
x86 is somewhat resistant to unsafe publishing. In particular, as long as one thread writes only to shared memory and the other only reads, the processor processes all the loads and storages, as if Java volatile semantics were in the memory cells. Records are never reordered over previous records, and reads are never reordered in past reads, so the read stream always sees the records in the correct order
But:
records can be moved after reading, which will be released later in the program. In this sense, volatile is still stronger than x86 promises
this is just what the processor can do with your code. The virtual machine can still reorder the code as it sees fit. For example, he can rewrite xb = 4; ya = 5; xb = 4; ya = 5; in ya = 4; xb = 5 ya = 4; xb = 5 .
Such a decision can be made on the basis of many different factors: the choice of parts of the code will be JIT-compiled, embedded, planned ... Therefore, even on processors with strong memory ordering, an unsafe publication remains unsafe.
source share