• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
noatom

What's the effect of a member volatile function?

5 posts in this topic

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

0

Share this post


Link to post
Share on other sites

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 ;)

0

Share this post


Link to post
Share on other sites

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

 

I stand correctified.

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0