0

I have the following c++ source test compiled by 4.8.3 :

void setCanceling( const char* OrderNoptr ){
    if( strcmp( BuyBook.OrderNo,OrderNoptr ) == 0 ){
        BuyBook.seqno++ ;
        asm volatile("":::"memory") ;
        BuyBook.Canceling = 1 ;
        asm volatile("":::"memory") ;
        BuyBook.seqno++ ;
    }
}

I won't like source to be modified by compiler optimizer to this :

void setCanceling( const char* OrderNoptr ){
    register int r = BuyBook.seqno ;  
    if( strcmp( BuyBook.OrderNo,OrderNoptr ) == 0 ){
        BuyBook.Canceling = 1 ;
        r += 2 ;
    }
    BuyBook.seqno = r ;
}

Is that possible asm volatile("":::"memory") can prevent such a modification by compiler in my test ?!

barfatchen
  • 1,630
  • 2
  • 24
  • 48
  • Possible duplicate of [How to prevent GCC from optimizing out a busy wait loop?](http://stackoverflow.com/questions/7083482/how-to-prevent-gcc-from-optimizing-out-a-busy-wait-loop) - have you tried and checked that it works or doesn't work? – Ken Y-N Oct 07 '16 at 03:51
  • The only reason I can think of that you would need something like this has to do with multi-threading. Perhaps you need to look at some of the atomic functions. – David Wohlferd Oct 07 '16 at 10:55

0 Answers0