2 ms·
A question: Why is it the CPU architecture that is weakly ordered, if it's the compiler that is reordering the statements? Couldn't you have a compiler on a wea
by usea 14y ago
A question: Why is it the CPU architecture that is weakly ordered, if it's the compiler that is reordering the statements? Couldn't you have a compiler on a weakly ordered arch that preserved order, and a compiler on x86 for example that could reorder your statements?
Isn't it the language spec / compiler that is in charge of this, rather than the CPU? I'd like to know more about this.
- andrewaylett 14y agoIn this case, the compiler could have re-ordered the instructions, but he checked to ensure that it hadn't.
- sigjuice 14y agoThis is not about the compiler reordering statements. This happens in hardware. If a core executes two store instructions, another core might see them in any order.
- klodolph 14y agoBoth the compiler and the CPU can reorder code, as long as the results don't change from the perspective of the code that is executing. For example, a compiler could take the following code: global_x = 1; global_y = 2; global_x = 3; And change it into: global_x = 3; global_y = 2; Compilers on all platforms reorder statements like this. You can prevent this by making your variables `volatile`, but 99% of the time when you write the word "volatile" in your code you're making a mistake.
- rayiner 14y agoIt's not just the compiler reordering statements. The CPU can reorder loads and stores that appear in the machine code.