The problem with variable shared by different Thread is in fact how the Compiler optimise the code.
You can have a situation where the variable is compiled in such a way that the code will access it via the cache, instead of accessing it thought its RAM location, and therefore one instance will have a totally different value from another one...
To avoid this situation there is a "C" and "C++" keyword which tells the compiler to not optimise the location of the variable. This keyword is "volatile".
In other word, whenever a variable (int, double, struct, static,....) must be accessed from more than one thread, add the volatile keyword to its declaration.
examples:
typedef struct
{
int val1;
int val2;
}myStruct_DEF;
volatile int myShar
edValue;
volatile myStruct_DEF mysharedStruct;
Note 1:
when you declare a structure volatile, all its members are automatically set to volatile.
Note 2:
You might get some casting error while accessing some volatile variable, in this case add a cast of the original type:
Example:
volatile int mySharedValue;
.....
int MyshareIntValue;
MyshareIntValue=(int)mySharedValue;
....
Concerning your 2nd question, you can declare volatile static variable without any problems...
static volatile int MyStaticSharedValue.
For the 3rd comments, I wouldn't say of "very limited use", that depends on the program....
Best regards.