use of keyword volatile in multi-threaded C++ program

C

C++Liliput

It seems that the keyword "volatile" is used to make sure that threads
reading (or writing to) the same data should see a consistent picture
of the variable i.e. updates made to the common data should
immediately get reflected to other threads that are using it. However
I have never faced this issue in y multi-threaded code even though the
common variables are not declared volatile. So is this just a matter
of coincidence that I have had no runtime issues or is it that the
"volatile" keyword is required in some special situations that perhaps
my code does not encounter? I would love to know a demonstrable piece
of code that really shows the relevance of volatile variables in multi-
threaded environment.
 
R

red floyd

C++Liliput said:
It seems that the keyword "volatile" is used to make sure that threads
reading (or writing to) the same data should see a consistent picture
of the variable i.e. updates made to the common data should
immediately get reflected to other threads that are using it. However
I have never faced this issue in y multi-threaded code even though the
common variables are not declared volatile. So is this just a matter
of coincidence that I have had no runtime issues or is it that the
"volatile" keyword is required in some special situations that perhaps
my code does not encounter? I would love to know a demonstrable piece
of code that really shows the relevance of volatile variables in multi-
threaded environment.

volatile simply tells the compiler not to optimize out accesses to a
variable. It's very useful for things like memory-mapped hardware
registers.

Note that you can have a const volatile. One of my favorite
declarations from some real-life code I wrote was:

unsigned long const volatile * const HW_RDONLY_REG =
reinterpret_cast<unsigned long const volatile *>(
0xC0008020);


Volatile does not guarantee asynchronous concurrent access consistency.
 
G

gpderetta

C++Liliput said:
It seems that the keyword "volatile" is used to make sure that threads
reading (or writing to) the same data should see a consistent picture
of the variable

[Your question is off-topic here, you should have asked it in
comp.programming.threads.]

Is it? Threading is in the draft standard and in a couple of years it
will be *the* standard. Aren't discussions about features present in
the upcoming standard on-topic here?
Such use of 'volatile' is always erroneous (or at a minimum
non-portable) -- volatile does *not* ensure "consistent picture of
the variable" on many common non-x86 machines.

In fact it doesn't even on x86 machines.
 
J

James Kanze

It seems that the keyword "volatile" is used to make sure
that threads reading (or writing to) the same data should
see a consistent picture of the variable
[Your question is off-topic here, you should have asked it
in comp.programming.threads.]
[/QUOTE]
Is it? Threading is in the draft standard and in a couple of
years it will be *the* standard. Aren't discussions about
features present in the upcoming standard on-topic here?

Even if it weren't part of the upcoming standard, it's part of
portable C++. More generally:

-- questions about precise details of the threading API of a
particular system belong in a group for that system,

-- general questions about threading (when to use it, how to
decide what goes into which thread, etc.) are probably more
appropriate in comp.programming.threads, but I wouldn't
really ban them here either, and

-- questions about the meaning of certain C++ keywords (like
volatile) in a threaded context are definitely on topic
here, and probably no where else.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top