Re: Substitute for volatile



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

vijay.rajamanickam@xxxxxxxxx wrote:
> I am a newbie w.r.t. compiler optimizations and I believe these
> optimizations

/which/ optimizations?

> are generally done in function level.
> Can I safely assume that if I use getter & setter functions,
> I can effectively substitute using the qualifier volatile
> for variables in all instances. Thanks in advance

I'm not certain of what you are asking here.

Perhaps I can answer you this way:

"volatile" is a hint to the compiler that the contents of a variable may
change out-of-band, and that it should not make assumptions about the
variable's contents in it's optimizations. It doesn't matter whether the
optimizations are at a function level, or at some other level;
"volatile" says don't make them.

The basic assumption that "volatile" disables is the one that the
compiler can keep track of variable initializations and discard
unnecessary initializations silently. This sort of behaviour occurs when
the variable is set with a value, and the value isn't seen to change
before the next reference to the variable. In a case like that, the
compiler is permitted to discard related operations so long as the
appearance of continuity is preserved.

For instance, given the code fragment...

{
int a = 10; /* A */
a = 10; /* B */
if (a == 10) return; /* C */
}

Since variable "a" is initialized to 10 at line "A", and no intervening
changes are made to the value of "a" between line "A" and line "B", the
compiler may assume that variable "a" has a value of 10 at line "B". It
may then discard the initialization of line "B" completely, because the
end condition of line "B" (a having the value of 10) is already guaranteed.

Similarly, since "a" is still guaranteed to have the value of 10 at line
"C", the compiler may optimize away the test (a == 10) and substitute a
TRUE value for it, and may even further optimize away the entire if
statement (because it is always TRUE) and just generate the code for the
return statement.

With volatile,

{
volatile int a = 10; /* A */
a = 10; /* B */
if (a == 10) return; /* C */
}

the compiler is forbidden to optimize away either line B or line C

Please note that "volatile" is not the default behaviour; if you don't
need "volatile", you don't have to use it.

So, when would you need "volatile"? You'd need it when the variable may
change asynchronously to the flow of the program. Typically, this comes
from the activities of a signal handler, which can run asynchronously to
the main code. There are also per-platform cases where volatile may be
necessary, but these are not explicitly covered by ISO C.


HTH

PS, your reference to "getter" and "setter" functions leads me to
question whether you are programming in C (the topic of comp.lang.c) or
C++. If you have a C++ mindset, then I must warn you that C is a
different language than C++, and while you can write OO code in C, most
do not.


- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDw7cXagVFX4UWr64RAu/MAKDGoKmH9Drn4YhONvGZx/Juovu9ogCguRo8
OxWGBQn+BGeLrJpYAgRc2Wk=
=lhdH
-----END PGP SIGNATURE-----
.



Relevant Pages

  • Re: Volatile + multithreading
    ... The volatile keyword has little use in multithreaded programming. ... > field or global seems to have no impact because it seems the compiler will ... operations as "special" and suppress optimizations around them. ... > majority of the multithreading issues that could have resulted from ...
    (microsoft.public.vc.language)
  • Re: [OT] volatile in userspace
    ... Without 'volatile' and disabling optimizations altogether, ... 'Compiler over-optimisations and "volatile"'), volatile is used to ... Well, if you look at the Wiki, it admits that this is a bug: ...
    (Linux-Kernel)
  • Re: MSDN volatile sample
    ... Could you write down the code which is the *optimized* code by compiler ... will make thread1 deadlock without read the actual updated value of variable ... Telling us we have to put volatile keyword to thread ... To prevent optimizations, you specify a variable as volatile. ...
    (microsoft.public.vc.language)
  • Re: Share .cpp and .h along projects
    ... larger object can be controlled in a threadsafe manner using a volatile ... pointer and memory barriers. ... The compiler DOES apply those optimizations. ...
    (microsoft.public.vc.language)
  • Re: Is the following code MT-Safe?
    ... the assert (which is a fundamental design error: ... and almost always leads to either major synchronization failures ... >Does _bRunning need to be tagged as volatile? ... compiler to cache values, but only during the execution of a function; ...
    (microsoft.public.vc.mfc)