aruna wrote:
What are the advantages of using volatile?
Here are some:
1. The compiler does not "optimize-out" the variable that
is declared as volatile.
2. The compiler refreshes variables marked as volatile
before using them.
What are the practical situations where you use
volatile specifier?
My experience, in embedded systems, is to use volatile
when the hardware can change the value of a memory
location without the program's consent or knowledge.
I/O devices, such as a UART, is one example. The
value in the Receive Register depends on the other
end of the connection. A host computer could send
a character over and it resides in the Receive
Register, without the knowledge of the executing
program.
Sensors and triggers are also another example.
The sensor could be triggered without the program's
knowledge.
Message queues are another one. Your program could
receive a message from another task without any
prior knowledge and at unexpected times.
Memory locations that are shared by your program
and either a hardware device or another task.
Stuff could be placed into the shared memory
without your program knowing about it.
See also:
mutex
lock
semaphore
signal
I've seen replies about volatile being used for
multi-threading, but that is outside my knowledge
base. :-)
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book