This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH][RFC] Remove volatile from data members in libstdc++


Boehm, Hans wrote:
> I'm not at all sure we're understanding each other correctly, here.
> 
> I'm saying that if I write, say:
> 
> unsigned global;
> 
> unsigned f(void)
> {
>   unsigned local = global;
>   unsigned i, sum = 0;
> 
>   for (i = 0; i < local; ++i) sum += i;
>   ...unrelated code that doesn't touch i, sum, local, or global ...
>   for (i = 0; i < local; ++i) sum -= i;
>   return sum;
> } 
> 
> then f may return a nonzero value since the second reference to local
> may involve an implicit reload of local from global, and hence may not
> get the same value as the first time around.  I claim that doesn't make
> the load from global look very atomic, though it perhaps fails in a way
> that different from what you might have expected.

This function, as written, will always return zero.  There is no
"implicit reload of local from global".  This function reads global
once, stores it in local, and that's that.  It doesn't matter whether or
not global is volatile.

If local is volatile, the situation is more complex.  Here, the compiler
should reload local each time -- but not by rereading global, just by
reloading from the address of local.  If local isn't placed in memory
(and nothing says it must be) then even that won't happen.  In practice,
using volatile for local, non-static variables isn't often useful,
except in the presence of setjmp.

-- 
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]