473,387 Members | 1,757 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,387 software developers and data experts.

MSDN volatile sample

200 100+
Hello everyone,


In the MSDN volatile sample,

http://msdn2.microsoft.com/en-us/lib...fd(VS.80).aspx

I do not understand what is the purpose of the sample. I have tried to remove the keyword volatile, and the result is the same. :-)

Any ideas?


thanks in advance,
George
Dec 28 '07 #1
6 1992
weaknessforcats
9,208 Expert Mod 8TB
All the volatile keyword means is that the variable can be changed by forces not under control of the compiler. Therefore, the compiler should not make assumptions about that variable, like stick it in a register where it's inaccessible to those outside forces. volatile is your recommendation that the variable should reside in memory.

volatile is the counterpart to the register storage class specifier where your recommendation is that the variable should reside in a register.
Dec 28 '07 #2
oler1s
671 Expert 512MB
Google is always useful: on the first page of results, I found a good description: http://www.space.unibe.ch/comp_doc/c.../volatile.html
Dec 28 '07 #3
George2
200 100+
Thanks oler1s,


I have read the link and it looks good. But I think volatile only useful for hardware related topics.

For the multiple threaded sample provided by MSDN, I think if we removing the volatile keyword and at the same time the behavior is different compared with when we use the keyword, then there is a wrong optimization. Then, in order to let shared data work well understand multi-threaded environment, we have to add volatile to all the variables we need to share -- but actually I have never seen people add volatile keyword to all shared variable.

Any comments?

Google is always useful: on the first page of results, I found a good description: http://www.space.unibe.ch/comp_doc/c.../volatile.html

regards,
George
Dec 29 '07 #4
George2
200 100+
Thanks weaknessforcats,


From general point of view, I agree and understand the function of the volatile keyword.

But in the multiple threaded sample provided by MSDN, I think if we removing the volatile keyword and at the same time the behavior is different compared with when we use the keyword, then there is a wrong optimization. Then, in order to let shared data work well understand multi-threaded environment, we have to add volatile to all the variables we need to share -- but actually I have never seen people add volatile keyword to all shared variable.

Any comments?

All the volatile keyword means is that the variable can be changed by forces not under control of the compiler. Therefore, the compiler should not make assumptions about that variable, like stick it in a register where it's inaccessible to those outside forces. volatile is your recommendation that the variable should reside in memory.

volatile is the counterpart to the register storage class specifier where your recommendation is that the variable should reside in a register.

regards,
George
Dec 29 '07 #5
oler1s
671 Expert 512MB
I have read the link and it looks good. But I think volatile only useful for hardware related topics.
In a sense, everything is hardware related. Last time I checked, programs ran on hardware, not thin air. The volatile keyword isn't used except at mostly a more advanced level. That's because you can get away with limited hardware knowledge for quite a while. Once you get into more advanced programming, with threading, or system benchmarking, or embedded programming, details matter.

For the multiple threaded sample provided by MSDN, I think if we removing the volatile keyword and at the same time the behavior is different compared with when we use the keyword, then there is a wrong optimization.
Incorrect. It is your responsibility as a programmer to understand how to write correct code. You cannot write bad code and hope that the compiler will "optimize" away the troubles. I have mixed feelings about learning assembly. On one hand, it's getting less and less useful to program at that level. On the other hand, trying to program an assembly teaches programmers a swift lesson, many lessons.

Let me be clear: the compiler does not do work for you. It does not write code for you. It should not. You write the code, the compiler tries to convert it properly. volatile says something about the hardware that is quite exceptional. It is your responsibility to figure out when this exception might occur.

One more thing: Do not spam multiple forums with the same question: http://forums.microsoft.com/MSDN/Sho...04897&SiteID=1
Dec 29 '07 #6
George2
200 100+
Thanks for your advice, oler1s!


In this discussion, I feel Visual Studio compiler will do some wrong
optimization, which assumes that loop control variable will not be changed by
another thread (I think this is what MSDN tells us). But actually, when we
write multi-thread programs, we often use a bool variable to control the loop
of a thread to determine whether it needs to stop, and the value of the bool
variable is changed/controlled by another thread (very often if we have a
status bar animation for a time consuming operation).

For such bool variable, I seldom see people add volatile to the bool
variable, does it mean it is wrong code from our study of the MSDN sample? I
feel my experience of multi-threaded programming is conflicted by reading the
sample.

Are they wrong code? Any comments?

In a sense, everything is hardware related. Last time I checked, programs ran on hardware, not thin air. The volatile keyword isn't used except at mostly a more advanced level. That's because you can get away with limited hardware knowledge for quite a while. Once you get into more advanced programming, with threading, or system benchmarking, or embedded programming, details matter.

Incorrect. It is your responsibility as a programmer to understand how to write correct code. You cannot write bad code and hope that the compiler will "optimize" away the troubles. I have mixed feelings about learning assembly. On one hand, it's getting less and less useful to program at that level. On the other hand, trying to program an assembly teaches programmers a swift lesson, many lessons.

Let me be clear: the compiler does not do work for you. It does not write code for you. It should not. You write the code, the compiler tries to convert it properly. volatile says something about the hardware that is quite exceptional. It is your responsibility to figure out when this exception might occur.

One more thing: Do not spam multiple forums with the same question: http://forums.microsoft.com/MSDN/Sho...04897&SiteID=1

regards,
George
Dec 30 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: newsock | last post by:
Why need to qualify a member function "volatile"? Why need to qualify a object "volatile"? When need to "const_cast" away "volatile" of an object and a member function? I also saw some code...
8
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;
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...
14
by: Ian Pilcher | last post by:
It's pretty common to see declarations such as: static volatile sig_atomic_t caught_signal = 0; C99 defines sig_atomic_t as a "... (possibly volatile-qualified) integer type of an object that...
14
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...
94
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...
3
by: arun.darra | last post by:
Hi, Is the following thread safe?? class Sample { private: volatile int number; public: Sample():number(0) {}
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...
8
by: CJ | last post by:
The following tries to make thread-safe code while avoiding the high cost of a lock. But is it kosher? // file or global scope level int X; volatile _Bool IsInitializedX=false;
7
by: George2 | last post by:
Hello everyone, In the MSDN volatile sample, http://msdn2.microsoft.com/en-us/library/12a04hfd(VS.80).aspx I do not understand what is the purpose of the sample. I have tried to remove...
0
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,...
0
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$) { } ...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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,...
0
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,...
0
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...

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.