-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thu, May 04, 2006 at 02:45:50PM -0700, Gary Funck wrote: > Beginning with this simple example, > > 1 int j; > 2 volatile int jv; > 3 void p() > 4 { > 5 ++j; > 6 ++jv; > 7 } > > when compiled with "gcc (GCC) 3.4.4 20050721 (Red Hat 3.4.4-2)" > the following code results: > > incl j > movl jv, %eax > incl %eax > movl %eax, jv [...] > Is there a technical reason that the use of "volatile" would > dictate the second form of increment that first loads the > value from memory into a register? I would think that a > systems programmer might expect the opposite behavior, where > "volatile" would imply the single instruction form of increment > (which is non-interruptible on single processor systems).
Systems programmers should know better than to expect a particular implementation of volatile. :) How, for example, would you suggest GCC generate code for this? volatile int qwerty; void p() { printf("qwerty = %d\n", ++qwerty); } You could get a (uniprocessor non-interruptible) single-instruction incl qwerty but then you'd have to read the value again to be able to print it: movl %eax, qwerty at which point you've lost your "one evaluation is one read cycle" semantics which some people might find even more important than (uniprocessor!) atomicity. Don't forget that if you really wanted SMP-safe modification of volatiles you'd have to use the "lock" prefix too. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (GNU/Linux) Comment: Please fetch my new key 804177F8 from hkp://wwwkeys.eu.pgp.net/ iD8DBQFEWwO1wyMv24BBd/gRAjZ8AKCj86xSMkLzuAGuE5uJPOxsaJqVSQCfdbPx /1Q+TLqzUE/p2vltjqOtk8I= =2Bcj -----END PGP SIGNATURE-----