Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Isn't this the purpose of well-defined atomic primitives?

After all, not just the compiler, but also the processor can reorder operations. So you have to annotate synchronizing memory operations regardless of whether the compiler is optimizing. e.g., a lock-free algorithm implemented using only volatile (what ACCESS_ONCE does), even with -O0, is almost certainly wrong.

The alternative to explicit annotation is for the compiler to generate full memory barriers around every memory access. That would indeed preserve semantics in a multithreaded context, at a ridiculous performance cost.



> well-defined atomic primitives

The example I gave is simple and relates to the example of the parent but there are more complex cases for which it is a matter of ongoing research to define a semantics that also admits compiler optimizations.

For example the "well-defined" semantics of (C|C++)11's atomics admits executions where values can materialize out of thin air [1].

The broader point I was hoping to make is that optimizations are great but are not free in a multi-threaded context with data-races (even benign ones). As a consequence the choice to just remove many of them is one that is supported by many people in the weak-memory community and even appears in newer memory models [2]. For example preventing read-write reorderings to prevent causal cycles.

[1] https://www.cl.cam.ac.uk/~pes20/cpp/notes42.html

[2] http://gee.cs.oswego.edu/dl/html/j9mm.html (ruling out po U rf cycles)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: