Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-28-2012, 05:33 AM   #1
Senior Member
Registered: Dec 2008
Posts: 4,732
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
[C++] Volatile object is often only used to synchronize access to non-volatile data.

From here:

volatile object is often only used to synchronize access to non-volatile data. Those accesses can still be reordered relative to the volatile ones.
What I already understand is that the contents of the
registers aren't visible outside a processor. Volatile
keyword is used when we want to tell the compiler that
this variable will be modified by some external source
so don't attempt to optimize it.

So, while multithreading on multiple processors at the,
we may want a thread from an another processor to edit
our variable, so we make it volatile to ask the compiler
not to store it in the registers.

Now, I need to understand what is the above quote
talking about - in detail please.

Last edited by Aquarius_Girl; 05-28-2012 at 05:43 AM.
Old 05-28-2012, 08:36 AM   #2
Nominal Animal
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Originally Posted by Anisha Kaul View Post
Now, I need to understand what is the above quote talking about - in detail please.
Say you have code which accesses three variables: first (which is not marked volatile), then volatile (which is marked volatile), then last (which is not marked volatile), in that order.

The quoted paragraph means that even if volatile is volatile, there is no requirement for the compiler to make sure that first is accessed first and last last; it is allowed to access the non-volatile variables whenever it sees best, for example both before the volatile access.

If you think about it, it should be obvious: since first and last are not marked volatile, the compiler can access them whenever it sees fit, how often or how rarely it deems necessary, regardless of where and when the source code refers to them. Just because you access a volatile variable does not impact how the other, non-volatile variables are accessed -- and that trips many programmers up.

In C and C++, one can use a volatile access to a "normal" variable, by casting the variable address to a volatile pointer, then dereferencing it. In C this is (*(volatile type *)&variable) and in C++ (const_cast<volatile type &>(variable) . It is just as effective as declaring the variable volatile, but only applies to the one access. This, and not using a different separate volatile variable, is the correct solution to guaranteeing a variable is really accessed then and there.

Does this clear it up for you, or should I elaborate?

Last edited by Nominal Animal; 05-28-2012 at 08:38 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
use of volatile utkarshrawat Programming 1 04-23-2012 02:34 AM
what is the specialty of volatile when we use it for registers or any where ? rpittala General 1 04-18-2012 01:43 AM
Samba with volatile profiles? adm1329 Linux - Server 2 06-14-2007 12:06 PM
NVM Non-volatile Memory access compent (API) yantai Linux - General 3 05-11-2007 01:17 PM
volatile kalleanka Programming 2 08-17-2006 08:38 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:00 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration