Skip to main content
Journey Uncommon Logo
JourneyUncommon
hardC++inlining and the as-if ruleSingle-choice MCQ

A compiler inlines a tiny accessor and then, under the as-if rule, hoists a load of a non-volatile global out of a loop because nothing in the loop body writes it. Another thread mutates that global concurrently. Why is this hoist legal from the standard's point of view?

// global, plain int (not atomic, not volatile) int g = 0; long sum(int n) { long s = 0; for (int i = 0; i < n; ++i) s += g; // g re-read each iter? return s; }