473,287 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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

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.
Sep 24 '08 #1
3 2306
C++Liliput wrote:
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.
Sep 24 '08 #2
On Sep 24, 7:22*am, Paul Pluzhnikov <ppluzhnikov-...@gmail.comwrote:
"C++Liliput" <aveekmi...@gmail.comwrites:
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.

--
Giovanni P. Deretta
Sep 24 '08 #3
On Sep 24, 12:50 pm, gpderetta <gpdere...@gmail.comwrote:
On Sep 24, 7:22 am, Paul Pluzhnikov <ppluzhnikov-...@gmail.comwrote:
"C++Liliput" <aveekmi...@gmail.comwrites:
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?
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.

--
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
Sep 24 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: spammy | last post by:
hi all, im trying to establish whether i have a race condition or critical section in the following. i have a dataaccess class that continually retireves a table from a sqlserver (which may be...
5
by: ben | last post by:
Hello All, I am trying to make sense of a bit of syntax, is there a guru out there that can clear this up for me. I have a buffer declared as static volatile u8 buffer; and I have a...
22
by: Assaf | last post by:
hi all i know that i should not cross-post, but i am not sure to which group to post this question. 2 quesions about volatile: 1. i use volatile when 2 threads access the same variable...
1
by: Graeme Prentice | last post by:
According to this web page http://www.jaggersoft.com/csharp_standard/17.4.3.htm making data volatile results in ordered reads and writes. Does this mean that the following (pseudo) code should...
13
by: yaron | last post by:
Hi all, let be focus on sigle processor machine 32 bits. 1. with multi-threaded on single processor machine 32bit do i have to sync access to atomic get/set properties of type less then 32 bits...
18
by: Mark | last post by:
Hi List, I want to write a function to copy some data out of a hardware buffer. The hardware can change the contents of this buffer without it being written to by my function. I want to use...
6
by: titan nyquist | last post by:
Can you make volatile structures in C#? I have a static class, to have "global" variables. This allows the whole program to see them. I make them "volatile" to avoid multi- threading accessing...
2
by: kishoremupparaju | last post by:
what is the use of volatile keyword in c language
2
by: lostlander | last post by:
I don't find the keyword "volatile" in C99 std.............. Where is it described exactly? thx!
3
by: Rakesh Kumar | last post by:
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.