Jump to content

  • Log In with Google      Sign In   
  • Create Account

What's the effect of a member volatile function?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 noatom   Members   -  Reputation: 782

Like
0Likes
Like

Posted 29 April 2013 - 02:01 PM

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

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

 

But,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?)



Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5380

Like
0Likes
Like

Posted 29 April 2013 - 02:09 PM

I guess so, since the function won't change whether it is called with volatile data or not.

 

But really volatile is so rarely used and even then doesn't really cut it for multithreaded stuff (which it is intended for), I wouldn't worry about it. About the best use for volatile these days is "really, don't optimise this out, I really want to see the value in the debugger". And even then it doesn't always work, so I have to go back to printf ;)


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3 tivolo   Members   -  Reputation: 955

Like
1Likes
Like

Posted 29 April 2013 - 03:13 PM

But really volatile is so rarely used and even then doesn't really cut it for multithreaded stuff (which it is intended for), I wouldn't worry about it.

 

Volatile was never intended for multi-threaded use, people just abused it. It won't work on PowerPC, or any architecture with a weakly-ordered memory model.



#4 Paradigm Shifter   Crossbones+   -  Reputation: 5380

Like
0Likes
Like

Posted 29 April 2013 - 03:26 PM

Good point, it was intended for hardware (where repeatedly reading same address may give different results).

 

I stand correctified.


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#5 frob   Moderators   -  Reputation: 21488

Like
1Likes
Like

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 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?


Edited by frob, 29 April 2013 - 05:19 PM.

Check out my personal indie blog at bryanwagstaff.com.

#6 noatom   Members   -  Reputation: 782

Like
1Likes
Like

Posted 29 April 2013 - 05:01 PM

I wasn't trying to solve any problem,I just read about them.I think every programmer should know as much as possible about the language he codes in.Even if some concepts might be never used by him.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS