Hi there All, I have a problem which seemed to be defying explanation, but I have come up with a theory. Could I possibly have some feedback on whether the following is likely, plausible, possible, untrue or downright rubbish? If one reads the contents of a CAN or ADC chip register at a particular address, then the label volatile is placed upon that address to prevent the compiler optimising out repeat readings of the address. If one reads the contents of the address into a variable, then the compiler would automatically treat the contents of this variable with similar care. Is it possible that there has been an oversight with statements where the contents of a variable depend on the contents of a volatile by way of an if statement, ie...
normal_var=volatile_var;
normal_var=voltile_var; if (normal_var=0x00) { another_normal_var+=1; }
Why wouldn't one just use a critical section for a variable shared amongst multiple priority ISRS and main run code?... or have ownership flags..... I'm a little confused on how using a volatile type qualifier protects you when using a shared resource?
Why wouldn't one just use a critical section for a variable shared amongst multiple priority ISRS and main run code? Because C doesn't have critical sections. or have ownership flags.. Because the objects in question are not owned by any of the threads of execution alone. They are shared by definition. Volatile is needed because, by default, a C compiler is supposed to know everything that's going on in a C program, so it can optimize things. If that presumption is broken, i.e. during the execution of a given code fragment, the state of any object used by it can change for a reason other than what's written out in that particular code fragment, you have to tell the compiler about that. And the volatile qualifier is the way you do that.