------- Comment #23 from davids at webmaster dot com 2009-02-25 18:35 ------- "Really to me this is still a valid transformation even in the inside threads. If you want to be able to access via different threads, use locks, heavy or light weight ones."
Absolutely, you do use locks everywhere you write to a variable that might be read from by another thread. The problem is that the compiler introduces locks where you *don't* write to a variable. It is simply impossible to use locks if the compiler can add a write where you didn't expect one. The classic example: if(can_write) acquire_lock(); for(int i=0; i<100; i++) { some_stuff(i); if(can_write) shared_variable+=i; } if(can_write) release_lock(); Here this code does acquire a lock if it plans to modify the shared variable. But the optimizations discussed will break this code. Also, you can have the same problem with this kind of code without threads. Imagine, for example, if the 'shared_variable' may be in read-only memory and 'can_write' indicates this case. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31862