Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volatile Member Functions (C++)

Given the following class:

class MyClass {
  public:
    int value() const {
      return value_;
    }

  private:
    volatile int value_;
};

Does the value() member function also have to be marked as volatile to avoid getting optimized away or is it okay as written? Thanks.

like image 402
Switch Avatar asked May 01 '12 13:05

Switch


People also ask

Can a function be volatile in C?

Volatile is used in C programming when we need to go and read the value stored by the pointer at the address pointed by the pointer. If you need to change anything in your code that is out of compiler reach you can use this volatile keyword before the variable for which you want to change the value.

Can we use volatile with function?

The volatile is used in different places. For memory mapped peripheral registers, some global variables, that are accessed by some other functions or interrupt service routines, or in some multi-threaded applications, the volatile can be used.

What is volatile member function in C++?

volatile means two things − - The value of the variable may change without any code of yours changing it. Therefore whenever the compiler reads the value of the variable, it may not assume that it is the same as the last time it was read, or that it is the same as the last value stored, but it must be read again.

What is member function in C?

Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static ; this is called a static member function.


2 Answers

It is completely analogous to how const works.

If you have a const object, only member functions marked const are callable.

And so...

If you have a volatile object, only member functions marked volatile are callable.

As long as the object itself is not volatile, it makes no difference whether the function is.

However, do keep in mind that volatile has nothing to do with multithreading, and it will not help you write thread-safe code. It is the wrong tool for anything concurrency-related.

like image 127
jalf Avatar answered Oct 18 '22 21:10

jalf


Does the value() member function also have to be marked as volatile to avoid getting optimized away or is it okay as written?

Marking the member function volatile will have no effect on whether it is optimized away or not. It is fine as written.

The worry is if I have MyClass c; and then call c.value(); a couple times, the compiler might think c.value() will return the same value (even though it could have possibly changed..)

It sounds like what you want is to learn about atomic variables. Take a look at std::atomic.

If you really want to learn about volatile, read this paper: http://www.cs.utah.edu/~regehr/papers/emsoft08-preprint.pdf

like image 42
Andrew Tomazos Avatar answered Oct 18 '22 21:10

Andrew Tomazos