Re: how to disable optimization for particular assignment statements?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Nov 21, 2013 at 12:55 AM, Marc Glisse <marc.glisse@xxxxxxxx> wrote:
> On Thu, 21 Nov 2013, Marc Glisse wrote:
>
>> On Thu, 21 Nov 2013, Andrew Makhorin wrote:
>>
>>> Hello,
>>>
>>> I have a C code like this:
>>>
>>> int foo(void)
>>> {     int phase;
>>>      . . .
>>>      phase = 1;
>>>      phase = 2;
>>>      phase = 3;
>>>      . . .
>>> }
>>>
>>> In case of -O0 gcc generates machine instructions for every
>>> assignment 'phase = ...'. But in case of -O2 gcc does not generate
>>> instructions for some assigments. Of course, this is correct. However,
>>> is there any way to tell gcc that 'phase' object is inspected by another
>>> thread, so it should not remove such statements?
>>
>>
>> volatile
>
>
> Well, volatile will prevent the operations from being removed. For proper
> synchronization with other threads, using atomic operations with the right
> synchronization parameter sounds better.

Basic rule of thumb: if code is not thread-safe, adding volatile will
never make it thread-safe.  That rule of thumb applies here.  Adding
volatile will not work, because it will not make the changes to the
value visible to other processors.


> http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html

Yes, if you have decided that this is what you really want to do,
using the atomic (or sync) builtins is the only viable approach.

Ian




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux