Hello everyone,
Through myself study and previous discussion here, I share the conclusion here that,
if some object is synchronized (mutex, critical section, etc.), there is no need to add volatile keyword.
Here is the reference,
(refer to section volatile, Critical Sections, and Race Conditions)
http://www.ddj.com/cpp/184403766
if my understanding is wrong or you have any other options, please feel free to add here.
thanks in advance,
George
6 1613
You should not need volatile for variables shared between threads provided you are not doing code optimizations. If you do an optimization, the variable may get slapped into a processor register and then when the context switch occurs, the value is written out with the switch and the not available to the incoming thread.
volatile insures the variable is created in memory.
Sorry, weaknessforcats.
I am not sure whether your reply has relationship with my question. :-)
The key point is if we add volatile to variable, in multi-threaded environment, there may be wrong result because of incorrect optimization of compiler/CPU for volatile variable, e.g. variable could be modified by another thread by reading only from register is not synced with memory.
My question is, in the article my understanding is correct or not in below two points,
1. if a thread shared data variable is in synchronization control (e.g. critical section or mutex), even if the variable if volatile, it is safe;
2. if the variable is outside synchronization control, we must add volatile to make thread shared data safe?
You should not need volatile for variables shared between threads provided you are not doing code optimizations. If you do an optimization, the variable may get slapped into a processor register and then when the context switch occurs, the value is written out with the switch and the not available to the incoming thread.
volatile insures the variable is created in memory.
regards,
George
The key point is if we add volatile to variable, in multi-threaded environment, there may be wrong result because of incorrect optimization of compiler/CPU for volatile variable, e.g. variable could be modified by another thread by reading only from register is not synced with memory.
What I said was you should not need volatile if you are not doing optimizations.
You reply was that you are doing optimizations. Therefore, you should use volatile. That will prevent the variable from being read from a register.
Am I missing something?
Sorry weaknessforcats, I have not made myself understood. :-)
The question is not about optimization, but about whether optimization (generated by non-volatile) will generate wrong code with wrong function.
The points from the article are,
1. We need to add volatile if we are not in synchronized area;
2. But we need not to add volatile if we are in a synchronized area.
Synchronized area, like critical section or mutex protected code segement.
I am coming here to ask question whether above points (1) and (2) are correct.
What I said was you should not need volatile if you are not doing optimizations.
You reply was that you are doing optimizations. Therefore, you should use volatile. That will prevent the variable from being read from a register.
Am I missing something?
regards,
George
That sounds correct.
If you are in a protected area, then the variable cannot change under your feet.
Video drivers are one place where I have seen volatile since the card alters memory that is also addressed by your program.
Thanks for all of your help on this thread, weaknessforcats!
I am happy that from your help, my question is answered.
That sounds correct.
If you are in a protected area, then the variable cannot change under your feet.
Video drivers are one place where I have seen volatile since the card alters memory that is also addressed by your program.
regards,
George
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Vladimir |
last post by:
Is synchronization for bool shared fields required in multithreaded
application?
Is there a cases when for example changing value from true to false in one
thread, when another thread is reading...
|
by: Tim Rentsch |
last post by:
Here's another question related to 'volatile'. Consider
the following:
int x;
void
foo(){
int y;
y = (volatile int) x;
|
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...
|
by: google-newsgroups |
last post by:
Hello,
even (or because?) reading the standard (ISO/IEC 9899/1999) I do not
understand some issues with volatile. The background is embedded
programming where data is exchanged between main...
|
by: Nils |
last post by:
Hi,
I have some problems with threads.
I have a class thats start a thread. After thread the class should wait
nutil the thread is finish. I tried it like this:...
|
by: cristalink |
last post by:
The below is perfectly OK in C++, but I wonder how safe is it C#. There are
cases when one thread is writing to, another one is reading from the same
variable. I don't want/need to synchronize the...
|
by: Samuel R. Neff |
last post by:
When is it appropriate to use "volatile" keyword? The docs simply
state:
"
The volatile modifier is usually used for a field that is accessed by
multiple threads without using the lock...
|
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...
|
by: George2 |
last post by:
Hello everyone,
Through myself study and previous discussion here, I share the
conclusion here that,
if some object is synchronized (mutex, critical section, etc.), there
is no need to add...
|
by: Dmitriy V'jukov |
last post by:
On 2 ΑΧΗ, 20:47, "Dmitriy V'jukov" <dvyu...@gmail.comwrote:
Q: Can I use Relacy Race Detector to check my algo againts other that C
++0x memory models (x86, PPC, Java, CLI)?
A Yes, you...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |