On Fri, 20 Jul 2018 at 23:06, Allan Sandfeld Jensen wrote: > > On Freitag, 20. Juli 2018 14:19:12 CEST Umesh Kalappa wrote: > > Hi All , > > > > We are looking at the C sample i.e > > > > extern int i,j; > > > > int test() > > { > > while(1) > > { i++; > > j=20; > > } > > return 0; > > } > > > > command used :(gcc 8.1.0) > > gcc -S test.c -O2 > > > > the generated asm for x86 > > > > .L2: > > jmp .L2 > > > > we understand that,the infinite loop is not deterministic ,compiler > > is free to treat as that as UB and do aggressive optimization ,but we > > need keep the side effects like j=20 untouched by optimization . > > > > Please note that using the volatile qualifier for i and j or empty > > asm("") in the while loop,will stop the optimizer ,but we don't want > > do that. > > > But you need to do that! If you want changes to a variable to be observable in > another thread, you need to use either volatile,
No, volatile doesn't work for that. http://www.isvolatileusefulwiththreads.in/C/ > atomic, or some kind of > memory barrier implicit or explicit. This is the same if the loop wasn't > infinite, the compiler would keep the value in register during the loop and > only write it to memory on exiting the test() function. > > 'Allan > >