That's why referencing the standard here is not really helpful. Should it not be noted that use of volatile variables results in undefined behaviour, except if atomic operations are guaranteed on a machine with the variable?
That's the pattern I already suggested. But what's the influence of volatile to that code? What would change if the indices were not attributed volatile?