473,548 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2316
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_cas t<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.comwr ote:
"C++Liliput " <aveekmi...@gma il.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.programmin g.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...@gmai l.comwrote:
On Sep 24, 7:22 am, Paul Pluzhnikov <ppluzhnikov-...@gmail.comwr ote:
"C++Liliput " <aveekmi...@gma il.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.programmin g.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.programmin g.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 objektorientier ter Datenverarbeitu ng
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
1334
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 slow). it provides a cached copy of this table to clients, for them to read from (note: there are no writes). the idea being if a particular table...
5
7902
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 pointer to that buffer declared as
22
2264
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 is this the proper use of volatile?
1
3285
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 work (assuming flag1 and flag2 are volatile) // thread 1 flag1 = true; if ( !flag2 )
13
1892
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 ? 2. for those properties is it enough to use volatile on single processor machine ? 3 does volatile solve the problem for multi-processor machine...
18
21487
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 memcpy to unload the data. Do I need to specify the source data as volatile in this case? What is the correct syntax for specifying that the data...
6
3525
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 issues. That works. THE PROBLEM: In that static class, I want to combine some variables inside a structure, and then make a variable of that...
2
4369
by: kishoremupparaju | last post by:
what is the use of volatile keyword in c language
2
1456
by: lostlander | last post by:
I don't find the keyword "volatile" in C99 std.............. Where is it described exactly? thx!
3
3591
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 about the language / usage rather than any thread specific question. Sorry - if posted my mistake. I understand that in C++ volatile objects (...
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7444
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7711
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7954
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5367
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5085
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.