I do not understand the use of volatile in the ALock class of Figure 7.7.
The declaration
volatile boolean[] flag
means, that the reference to the array (nothing else [?]) is volatile. The footnote says, that the role is *not* to introduce a memory barrier (which it doesn't) but prevent optimizations in the loop
while (!flag[slot]) {}
It do not get it. The only optimization would be to not go to the memory location of flag[slot]. If that should be prevented, that would mean "memory barrier", no? In addition, one thread with say slot==17 does set flag[slot+1] (flag[18]) which in turn is read by another thread; Thus one thread modifies a value without memory barrier that is read by another thread. Isn't that speculation about unsynchronized access? Is that guaranteed to work? I would understand the algorithm if each value of the array would be volatile; if not, I do not get it. Shouldn't it be AtomicReferenceArray to always work correctly?
Thanks for any hints!