This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC 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: Curious global variable bug? Bad assumptions on my part?


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



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