Are x86 / x64 architectures resilient to unsafe publishing?

I noticed that unsafe publishing of objects in the JVM 1.5+ is unlikely to cause any problems, regardless of the JVM specifications, stating that it is not guaranteed that such objects will be visible between threads.

While browsing the Internet, I found this: http://forum.springsource.org/archive/index.php/t-60676.html A person with the nickname " al0 " that "[...] is unlikely to meet this behavior on "x86 / x64-based machines, but on HP PA-RISC or IBM Power ... computers (like the AS400) it’s much more likely."

Are x86 / x64 architectures resilient to unsafe publishing? Why?

+4
source share
2 answers
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.

+3
source

I do not want to assume whether the problem will be more or less likely on x86 / x64 processors. The claim that it is very unlikely that this behavior on x86 / x64 processors is incorrect.

Niklas Schlimm wrote an article about this in DZone with a working example to demonstrate improper behavior if a variable with multi-threaded access is not declared mutable. He writes in an article that he can only reproduce behavior from a VM server, but for me the test also does not work with a client virtual machine (Oracle Java 7, Intel x64 Mobile CPU). It is important to remember that this depends, at least on the exact version of the Java VM and CPU model, if the behavior is reproduced at all or perhaps even sporadically.

+1
source

Source: https://habr.com/ru/post/1499617/


All Articles