3 ms·
You should.be able to enforce it with the memory ordering supported since either C++11 or 14 (can’t recall which). It wouldn’t be quite as easy to write down, b
by aardvark179 7y ago
You should.be able to enforce it with the memory ordering supported since either C++11 or 14 (can’t recall which). It wouldn’t be quite as easy to write down, but might provide stronger guarantees than the volatile semantics.
- the8472 7y agocompilers are still allowed to optimize atomics, e.g. eliminating redundant reads. they are not with volatile, e.g. when you need exactly-once semantics for IO ports.
- asveikau 7y agoI disagree. Guaranteeing that the system behave as if operations have relative ordering is not the same as making sure an operation explicitly happens due to its side effects. They are entirely separate problems. A valid optimization for the former but not for the latter would be to coalesce redundant operations.
- FartyMcFarter 7y agoHow would you do something like this [1] using those? [1] https://godbolt.org/z/Aj5bJ8 https://godbolt.org/z/Aj5bJ8
- amluto 7y agoI would argue that this particular example is useless. A volatile variable at file scope can easily be IO memory. A volatile static at function scope? Not so easy.
- FartyMcFarter 7y agoIt can be set by a debugger for example. In any case, the scope of the variable was not the point of the question. The behaviour doesn't really change if you make the variable a static file-scoped one.