468,496 Members | 1,828 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

volatile keyword for C++ member functions

Hi -
I am actually trying to get my feet in multi-threaded C++
programming. While I am aware that the C++ standard does not talk
about threads (at least, for now - in C++03) - my question is more
about the language / usage rather than any thread specific question.
Sorry - if posted my mistake.

I understand that in C++ volatile objects ( those non-primitive type
instances qualified with 'volatile' ) can actually call only those
member functions qualified as volatile .

But there is also a construct - const_cast< T *(p) , that can
convert a volatile object pointer to constant pointer that can be used
to invoke non-volatile methods on the class.

That being the case - what would be the significance of the
'volatile' keyword qualified for c++ member functions in particular -
if we are able to invoke the non-volatile functions by casting. Am I
missing a design picture here.
Nov 30 '07 #1
3 3258
Rakesh Kumar wrote:
Hi -
Please don't multi-post on Usenet, this probably belongs in the other
group you posted to.

--
Ian Collins.
Nov 30 '07 #2
On Nov 30, 7:52 am, Rakesh Kumar <rakesh.use...@gmail.comwrote:
I am actually trying to get my feet in multi-threaded C++
programming. While I am aware that the C++ standard does not talk
about threads (at least, for now - in C++03) - my question is more
about the language / usage rather than any thread specific question.
Sorry - if posted my mistake.
I understand that in C++ volatile objects ( those non-primitive type
instances qualified with 'volatile' ) can actually call only those
member functions qualified as volatile .
More precisely: given a volatile qualified lvalue, you can only
call volatile functions on it.

Similar rules affect reference binding and implicit pointer
conversions. A volatile lvalue can only be bound to a reference
to volatile, and a volatile pointer will not convert implicitly
to a non-volatile one.
But there is also a construct - const_cast< T *(p) , that can
convert a volatile object pointer to constant pointer that can be used
to invoke non-volatile methods on the class.
The same rules apply as for const. If the actual object is
volatile, and it is accessed through a non-volatile lvalue, the
results are undefined behavior.

Thus, for example:

Toto volatile t1 ;
Toto t2 ;

void f( Toto volatile& t )
{
const_cast< Toto& >( t ).someFunction() ;
}

// ...
f( t1 ) ; // causes undefined behavior
f( t2 ) ; // no problem, according to the standard.
That being the case - what would be the significance of the
'volatile' keyword qualified for c++ member functions in particular -
if we are able to invoke the non-volatile functions by casting. Am I
missing a design picture here.
What is the signification of const, given that you can invoke
non-const functions by casting? The rules are the same.

More to the point, since you started out by talking about
threading: what is the significance of volatile with regards to
threading? Posix makes no guarantees concerning volatile and
threading; I don't know what Windows "guarantees", but the
current VC++ compiler doesn't generate anything that could
possibly be useful for threading. (Andrei once wrote an article
in which he used volatile to ensure correctly locked accesses,
but his code worked not because it used any of volatiles
semantics, but because it exploited the way volatile works in
the type system.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 30 '07 #3
"James Kanze" <ja*********@gmail.comwrote in message
news:67**********************************@s36g2000 prg.googlegroups.com...
On Nov 30, 6:39 pm, gpderetta <gpdere...@gmail.comwrote:
James Kanze wrote:
[...]
More to the point, since you started out by talking about
threading: what is the significance of volatile with regards to
threading? Posix makes no guarantees concerning volatile and
threading; I don't know what Windows "guarantees", but the
current VC++ compiler doesn't generate anything that could
possibly be useful for threading.
Actually I think that VC++ generates proper memory barriers
for accesses to atomic volatile objects.
I've looked at the generated code from VC++ 8, and they weren't
there.
[...]

http://groups.google.com/group/comp....61a53de7c2290e

Humm...

Nov 30 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by newsock | last post: by
17 posts views Thread by Radde | last post: by
9 posts views Thread by mlimber | last post: by
13 posts views Thread by yaron | last post: by
2 posts views Thread by Christian Christmann | last post: by
6 posts views Thread by venky | last post: by
94 posts views Thread by Samuel R. Neff | last post: by
5 posts views Thread by Mark Salsbery [MVP] | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.