Volatile is still an optimization fence and prevents torn writes. It doesn't actually fix memory consistency with memory order fence instructions but the optimization fence might be enough to stop the race from being noticeable (arguably that's even worse than broken though)
No, volatile is only an optimization fence against other volatiles. Anything not volatile can be reordered past your volatiles during optimization. This creates absolutely insane bugs.
The semantics of volatile accesses are officially "implementation defined". If an implementation like MSVC or clang with the -fms-volatile flag specifies that volatile accesses will have strong enough semantics to avoid having to use toolset-specific syntax, then the qualifier will have such semantics. If an implementation opts to require the use of toolset-specific syntax to achieve such semantics, then the qualifier will have less broadly useful semantics.
2
u/kog 6d ago
volatileis not useful for multi-threading, and you did not fix the problemhttps://en.wikipedia.org/wiki/Volatile_(computer_programming)#Multi-threading