The trouble with volatile
Posted May 11, 2007 21:50 UTC (Fri) by
giraffedata (subscriber, #1954)
Parent article:
The trouble with volatile
Linus' conclusion may be right, but his reasoning is all wrong.
First, while the purpose (reason for existence) of "volatile" is to prevent optimization, that's not what it's definition is. The keyword "volatile" does not say to not optimize; it says the value may change all by itself. So the fact that disabling optimization is a poor goal is not an argument against using "volatile."
Then Linus misinterprets the English word "volatile" and claims that data isn't volatile; rather accesses are volatile. The opposite is true.
I think he should have instead pointed out that some data is known to be involatile some of the time, so it is better if you can declare specifically when the data is volatile rather than just say it is volatile in general.
But the ways provided are just as over-general in the other direction. They say in a certain interval every memory location is volatile.
There may be variables that due to circumstances cannot change while cpu_relax() runs. The program unnecessarily rereads memory.
I don't doubt that analysis shows that this over-generalization is better than the other over-generalization, but at least we should understand what comparison we're making.
(
Log in to post comments)