5 ms·
> No, because in practice that "wait until" operation will act as a memory barrier. This is a wrong, a memory barrier would not salvage this code from UB. The
by sapiogram 2y ago
> No, because in practice that "wait until" operation will act as a memory barrier.
This is a wrong, a memory barrier would not salvage this code from UB. The read from `x` must at the very least be synchronized, and there might be other UB lurking as well.
- ajross 2y agoNo, synchronization is a different issue entirely. The question upthread was whether it was OK for the compiler to optimize the code to return a constant zero without actually reading from the pointer. And it's not, because any reasonable implementation of "wait for" will defeat aliasing analysis. You're right that if you try to write async code with only compiler instrumentation, you're very likely to be introducing race conditions (to be clear: not necessarily on architectures with sufficiently clear memory ordering behavior -- you can play tricks like this in pure C with x86 for instance). But that wasn't the question at hand.