I just asked a question involving volatile: volatile array c++
However my question spawned a discussion on what volatile
does.
Some claim that w
It's worse than you think, actually - some compilers may decide that that loop is either a no-op or infinite loop, eliminate the infinite loop case, and make it return immediately no matter what done is. And the compiler is most certainly free to keep done
in a local CPU register and never access its updated value in the loop. You must either use appropriate memory barriers, or a volatile flag variable (this technically isn't enough on certain CPU architectures), or a lock-protected variable for a flag like this.