Consider the following code:
#include <atomic>
extern std::atomic<int> i;
void f(void)
{
while (!i.load(std::memory_order_relaxed))
;
}
I'm looking for a citation from the C++11 standard that says that the compiler is not allowed to transform the loop into
if (!i.load(std::memory_order_relaxed)) {
while (1)
;
}
I've seen some discussion here but nothing conclusive.
Edit: A previous version of this post called an extern function inside the loop.
Edit 2: For motivation: The book "Effective Java" says that the HotSpot VM performs the following transformation:
while (!done)
i++;
to
if (!done)
while (true)
i++;
even though it's perfectly defined behavior for another thread to change the done variable concurrently.