Curious global variable bug? Bad assumptions on my part?
Tom St Denis
tstdenis@ellipticsemi.com
Thu Feb 14 18:17:00 GMT 2008
Matthew Woehlke wrote:
> Eljay Love-Jensen wrote:
>> Adding volatile to the g_counter tells the compiler not to optimize away
>> access to the variable, as the variable can change "at any time".
>>
>> That may sound like just the thing for multi-threaded access to the same
>> variable -- but it's not sufficient.
>>
>> Even though the compiler is no longer going to optimize away access
>> to the
>> variable, the hardware can still effectively isolate the two threads
>> from
>> one another, such that neither thread is aware that the g_counter
>> variable
>> has changed.
>>
>> Why? Because each thread could run with the variable stored in separate
>> caches.
>
> Huh? I thought SMP hardware is required to be responsible for cache
> syncing in such an instance...
Not all platforms are your desktop. Think of embedded systems with no
snooping, the Nintendo DS is such a model iirc.
Even then, even with snooping, GCC could alias the variable to a
register [without volatile] in which case it would still not work.
Tom
More information about the Gcc-help
mailing list