jeudi 1 mars 2018

c++ volatile member variable

I'm working with c++ 11 and I encountered a class with 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 HW register we should use volatile, I mean a "pure SW" scenario)

Example:

class MyClass
{
public:
    FuncA { if (x==5) print("hello"); }
    FuncB { x=5;}
private:
    volatile int x = 0;
}

Different threads are accessing MyClass, 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"?

Thanks.

Aucun commentaire:

Enregistrer un commentaire