79420214

Date: 2025-02-07 08:39:19
Score: 1.5
Natty:
Report link

Is my understanding of the JLS / JMM correct in that the program above is allowed to not halt? If no, where is my mistake?

As far as I understand, that is correct.
The reason is that the JMM lacks requirements that volatile writes should become visible in another threads in some finite amount of time.

Here is how such requirements expressed in C++:

18 An implementation should ensure that the last value (in modification order) assigned by an atomic or synchronization operation will become visible to all other threads in a finite period of time.

11 Recommended practice: The implementation should make atomic stores visible to atomic loads, and atomic loads should observe atomic stores, within a reasonable amount of time.

It would be nice to add something like that to the JMM as well.


If my understanding is correct, is there some combination of circumstances (CPU architecture, JDK build, JVM arguments, ...) where the program given above, or a variant of it (as long as flag is volatile) does not halt on a real JVM?

I haven't heard about anything like that.

For any "serious" JVM (e.g. HotSpot, OpenJ9, GraalVM) such behavior IMO would be a bug, even if the math in the JMM permits that.

BTW strictly speaking a JVM where all volatile writes are never visible to other threads meets the JMM spec.

Reasons:
  • Long answer (-1):
  • No code block (0.5):
  • Contains question mark (0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: user29544191