memory_relaxed and VarHandler

80 views Asked by At

In https://shipilev.net/blog/2016/close-encounters-of-jmm-kind/#myth-barriers-are-sane the author wrote:

Of course, without keeping reads in order, the result 1, 0 is trivially achievable. But this does not make an interesting test case. The actual test is clever about that: it uses the new VarHandles "opaque" access mode, which inhibits these optimizations and exposes the reads to hardware in the same order:[3]

and [3]:

  1. This sounds very similar to C/C++11 std::atomic(…​, memory_order_relaxed), which is what it is modeled after. Very convenient for hardware concurrency testing, as in this example.

I am not sure what the author means because he says that

which inhibits these optimizations and exposes the reads to hardware in the same order

whileas

memory_order_relaxed allows every reordering (it doesn't give any special guarantees in memory ordering).

1

There are 1 answers

1
user21205071 On

You are correct: opaque isn't enough there because it only orders accesses to the same variable.

The same example in JCStress' source code uses volatile mode instead of opaque.