On Fri, 1 Dec 2023 13:09:52 GMT, Anton Bobrov <[email protected]> wrote:
> hey, a fellow Sun comrade! (i still have my Sun badge too) :)
:handshake:
> I dont think modern compilers would do that
I would argue that no standard-compliant compiler would do that.
`volatile` is very hard to summarize without loosing accuracy (see WG21
6.7.3/7 and Annex C) , but they are about hardware interrupts/signal handlers
and the like that can "suddenly" change the value ("modified in ways unknown to
the implementation"), not about multithreading and certainly neither replace
nor enhance synchronization.
> compiler might decide to otherwise optimize away the 'if' block in this loop
That would be a (huge) bug in the compiler. The variable is global, the rules
of the abstract machine allow for the value to change outside of the loop and
outside of the containing function.
FWIW, `sessionClosed` also doesn't have to be volatile.
> there is no real cost of using volatile in this case
I agree that this is hardly the hottest spot of the entire affair of taking a
screenshot, but the cost is in readability. `volatile` has a very special
meaning and here it is used for a purpose that does not match this meaning.
This is at least confusing. Just like you wouldn't use _Atomic in a
single-threaded application even though it might cost nothing overall, you
shouldn't use volatile simply because it's cost-less in terms of performance.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/16794#discussion_r1412120620