
Part: 18.13 Volatile Variables
In order to rectify this situation we declare variables to be volatile in order to disable register optimisations:
// Shared variable volatile int i2; // The same functions as above but use a volatile variable. // Not optimised anymore so results are always correct. void FV1() { while (true) i2++; } void FV2() { while (true) cout<<i2<<endl; }
This is so wrong!
Please explain why this is wrong. Is the code wrong? Is the example wrong? Please note that this is a snippet from the full test program where two different threads are started to execute FV1() and FV2().
In addition to what was told, read following. Read this: http://en.wikipedia.org/wiki/Volatile_variable#In_C_and_C.2B.2B And this: http://kernel.org/doc/Documentation/volatile-considered-harmful.txt Artyom