[fpc-devel] volatile variables

Jonas Maebe jonas.maebe at elis.ugent.be
Tue Jun 28 16:23:00 CEST 2011


On 28 Jun 2011, at 15:54, Michael Schnell wrote:

> static int x;
> void play_with_x(void) {
>  for (i=100000000; i>0; i--) {
>    x += 1;
>  };
>  x = 0;
> };
>
> the compiler will see that x is just defined to be 0 in the end and  
> optimize out thge complete loop.
>
> But if you do the same with
>
> volatile static int x;
>
> the code will stay and another thread can watch x growing in a time  
> sharing system.
>
> I believe that inserting some ptherad_mutex... calls will not force  
> the compiler to bother about some intermediate values of a non  
> volatile variable.

You believe wrongly.


Jonas



More information about the fpc-devel mailing list