4 ms·
The code is still incorrect, because the compiler is free to speculate and transform the code in a way that leads to availableData being loaded twice (as in the
by nhaehnle 10y ago
The code is still incorrect, because the compiler is free to speculate and transform the code in a way that leads to availableData being loaded twice (as in the macro). That probably doesn't happen in practice, but it is an allowed transform. You really do need to explicitly tell the compiler that it should load availableData only once (e.g. with a volatile load or an atomic load built-in).
- mikeash 10y agoIt was marked as volatile, so the compiler wouldn't be allowed to load it twice. I do actually know what I'm doing here....
- jjnoakes 10y agoYou probably know, but for others reading these threads: volatile is not the optimal way (and usually it isn't even a correct way, outside of very specific code) to synchronize variables across threads.
- mikeash 10y agoYeah, don't use what I've said as a guide for writing your own stuff. I'm pretty sure I did it right, but you need a lot more understanding of what's going on than what I've explained.... These days, with atomics showing up in the C and C++ standard libraries, there are better ways to do it altogether.