Re:What's up with this new memory_order_consume memory order?
by gpderetta on Thursday May 25, 2023 @03:45AM
(#36068078)
No, loads from x need not be consume nor even need to be atomic. If the thread that last wrote to x and then i never touches x again, there is no possible data race. This is for example the case of for initialization guards.
[ Parent
| on HN ]
Re:What's up with this new memory_order_consume memory order?
by kazinator on Thursday May 25, 2023 @11:34AM
(#36072084)
* * *
Re:What's up with this new memory_order_consume memory order?
by gpderetta on Thursday May 25, 2023 @12:04PM
(#36072498)
If p is like by a lock, the compiler won't reorder the access outside of the lock but it can still take into account that i is a known constant to simplify the address computation of x[i] into x[3]. This is perfectly safe for a mutex protected variable as the mutex acquire will, in addition to the compiler barrier, provide the required hardware barriers.
This is is catastrophic, if, instead of relying on an hardware barrier, we rely on a load-load dependency chain originating from i; rewriting x[i] into x[3] break the chain and now, while the compiler might still not reorder the access, the hardware very much can.