• Create Account

### #Actualfrob

Posted 29 April 2013 - 05:19 PM

You didn't specify the language.

The major languages have their own "volatile" keyword, but it means something very different in each language.

Assuming C++.

I know that inside a volatile function you can only call other volatile functions,nothing else.

Okay...

Let's look at it carefully.

class Foo {
public:
int f1() { x = 1; y = 1; return x;}
int f2() volatile {  x = 2; y = 2; return y; }

int x;
volatile int y;
};

int main()
{
volatile Foo a;
a.f1();  // Compile error, cannot call non-volatile method on volatile object
a.f2();  // OK

Foo b;
b.f1();  // OK
b.f2();  // Implicit conversion to cv-qualified object, OK
}


So,if I create a nonvolatile object that has a volatile member function,and call that function,what exactly happens?

That is the case for b.f2().

At compile time, the compiler performs an implicit 'qualification conversion'.  Since you don't implement a conversion function to volatile (they are super-extremely rare in real life, I've seen more goto statements than volatile conversion functions) all that happens is the this pointer inside f2() is volatile qualified.  The function executes normally.

I know that inside a volatile function you can only call other volatile functions,nothing else.

Correct.  Const and volatile qualifiers work the same way.  Just like a const-qualified function can only call other const-qualified functions, a volatile-qualified function can only call other volatile-qualified functions.

because the method is volatile,does that mean that it will treat all the member objects in that object as volatile too?(aka,read their values absolutely everytime?)

No, that is not what volatile means.

Many C and C++ programmers think volatile means something different than it does.

Volatile is a de-optimization.  It is a request (not a guarantee) for the compiler to not make certain optimizations.  It was originally designed for memory-mapped I/O registers that were modified either by the hardware or operating system.  It says "Please take steps to not cache this value; it may be used externally."

Volatile objects mean that a variable access must be done before the next sequence point.  Nothing more.

There is another keword, register, which means "try to store this object in a CPU register to speed up performance".  The volatile keyword is the opposite, "try to store this in memory and not on the CPU".  Really that means load/store optimizations generally get disabled.

Optimizations of merging, reordering, and conversions are allowed on volatile variables, and compilers generally take those optimizations.  Volatile does not mean atomic.  Volatile does not mean 'correctly' shared between threads or between CPUs or through the OS.  Volatile does not mean it should be a synchronization primitive.  It simply means the object should live in memory rather than on the CPU.

In a multithreaded environment, the very first thing you you should do with a volatile-qualified member function is to lock this and then call the non-volatile equivalent. This is because there is no control and any thread might interrupt any other at any time; you need to keep with the original intent of volatile -- do not unwittingly cache values used by multiple threads at once. For example:

void Widget::Operation() volatile
{
LockingPtr<Widget> lpThis(*this, mtx_);
lpThis->Operation(); // invokes the non-volatile function
} 

Hopefully that clears it up.  It has nothing to do with "read their values absolutely everytime".  It means "please store this object in memory and disable load/store optimizations".

So now that I've tried to carefully answer the questions you asked, here's two for you:

Why are you trying use volatile members this way?  What real-life problem are you trying to solve?

### #1frob

Posted 29 April 2013 - 04:35 PM

You didn't specify the language.

The major languages have their own "volatile" keyword, but it means something very different in each language.

Assuming C++.

I know that inside a volatile function you can only call other volatile functions,nothing else.

Okay...

Let's look at it carefully.

class Foo {
public:
int f1() { x = 1; y = 1; return x;}
int f2() volatile {  x = 2; y = 2; return y; }

int x;
volatile int y;
};

int main()
{
volatile Foo a;
a.f1();  // Compile error, cannot call volatile method on non-volatile object
a.f2();  // OK

Foo b;
b.f1();  // OK
b.f2();  // Implicit conversion to cv-qualified object, OK
}


So,if I create a nonvolatile object that has a volatile member function,and call that function,what exactly happens?

That is the case for b.f2().

At compile time, the compiler performs an implicit 'qualification conversion'.  Since you don't implement a conversion function to volatile (they are super-extremely rare in real life, I've seen more goto statements than volatile conversion functions) all that happens is the this pointer inside f2() is volatile qualified.  The function executes normally.

I know that inside a volatile function you can only call other volatile functions,nothing else.

Correct.  Const and volatile qualifiers work the same way.  Just like a const-qualified function can only call other const-qualified functions, a volatile-qualified function can only call other volatile-qualified functions.

because the method is volatile,does that mean that it will treat all the member objects in that object as volatile too?(aka,read their values absolutely everytime?)

No, that is not what volatile means.

Many C and C++ programmers think volatile means something different than it does.

Volatile is a de-optimization.  It is a request (not a guarantee) for the compiler to not make certain optimizations.  It was originally designed for memory-mapped I/O registers that were modified either by the hardware or operating system.  It says "Please take steps to not cache this value; it may be used externally."

Volatile objects mean that a variable access must be done before the next sequence point.  Nothing more.

There is another keword, register, which means "try to store this object in a CPU register to speed up performance".  The volatile keyword is the opposite, "try to store this in memory and not on the CPU".  Really that means load/store optimizations generally get disabled.

Optimizations of merging, reordering, and conversions are allowed on volatile variables, and compilers generally take those optimizations.  Volatile does not mean atomic.  Volatile does not mean 'correctly' shared between threads or between CPUs or through the OS.  Volatile does not mean it should be a synchronization primitive.  It simply means the object should live in memory rather than on the CPU.

In a multithreaded environment, the very first thing you you should do with a volatile-qualified member function is to lock this and then call the non-volatile equivalent. This is because there is no control and any thread might interrupt any other at any time; you need to keep with the original intent of volatile -- do not unwittingly cache values used by multiple threads at once. For example:

void Widget::Operation() volatile
{
LockingPtr<Widget> lpThis(*this, mtx_);
lpThis->Operation(); // invokes the non-volatile function
} 

Hopefully that clears it up.  It has nothing to do with "read their values absolutely everytime".  It means "please store this object in memory and disable load/store optimizations".

So now that I've tried to carefully answer the questions you asked, here's two for you:

Why are you trying use volatile members this way?  What real-life problem are you trying to solve?

PARTNERS