5 ms·
In x86 at least, 32 bit loads and stores are done atomically already.
by BubRoss 7y ago
In x86 at least, 32 bit loads and stores are done atomically already.
- likeliv 7y agoI know rust is not C or C++, but we are not programming in x86 assembly, but for the language "abstract machine". And if the compiler infer that this memory location is not used by other threads because we don't use an atomic operation, it can perform optimizations that could result in subtle bugs. That's what undefined behaviour is. Although in this case, I guess this is probably fine since the non-atomic read can't race with a write.
- gpderetta 7y agoThe code is correct both practically and formally. All unordeeed reads are reading values written by the same thread. Even if the writes are atomic there is no violation of the no data races rule.
- charliesome 7y agox86 does do out-of-order execution however, so while it's true that you'll never read a half-written value, it's still important to use the atomic types to ensure that reads/writes across threads are performed in the right order.
- BubRoss 7y agoThat's true but not what is being discussed here.