I'm working with C++ 11 and I encountered a class with a volatile member variable, which raised a question:
Question 1:
if a member variable is set in one class function and polled in another class function - is there ever a need for volatile? If so, when? (I know that if x is mapped to hardware register we should use volatile, I mean a "pure software" scenario)
Example:
class MyClass
{
public:
FuncA { if (x==5) print("hello"); }
FuncB { x=5;}
private:
volatile int x = 0;
}
Different threads are accessing MyClass
instance, but no 2 threads at the same time, so no need to guard x.
Question 2:
Is there a scenario (some kind of optimization) that after FuncB is called FuncA will not print "hello"?