Volatile specifier

A

aruna

What are the advantages of using volatile? What are
the practical situations where you use volatile specifier?
 
E

Eric Sosman

aruna said:
What are the advantages of using volatile? What are
the practical situations where you use volatile specifier?
[... and questions of a similar level in many more messages ...]

Aruna, have you done any research on these questions before
posting them? Have you read a C textbook or tutorial? Have
you read the comp.lang.c Frequently Asked Questions document?
If so and if you are confused by what these sources have told
you, *then* take your question to comp.lang.c, and explain
the nature of your confusion.

Do not try to treat comp.lang.c or any other forum as a
substitute for study. Usenet is an ineffective vehicle for
basic instruction.
 
T

Tim Prince

aruna said:
What are the advantages of using volatile? What are
the practical situations where you use volatile specifier?
Did you read any FAQs about this? What are your specific questions? I
won't mention my personal reasons for using it, as they aren't likely to
intersect with the interests of 98% of the readers of this NG, and don't
conform to Standard C.
 
C

Christopher Benson-Manica

aruna said:
What are the advantages of using volatile? What are
the practical situations where you use volatile specifier?

1) It's a "qualifier", not a "specifier".
2) Read a C book. Or the comp.lang.c FAQ. Or its archives.
 
T

Thomas Matthews

aruna said:
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
 
K

Karthik

Thomas said:
I've seen replies about volatile being used for
multi-threading, but that is outside my knowledge
base. :)

The fundamental concept remains the same there still. If multiple
threads are accessing a variable, it is important that the thread (when
it sees a variable x ) , actually gets the most recent value of x. This
would be taken care by the compiler to make sure that it refreshes
before fetching the value for the current thread, without doing any
optimization, thereby preserving the semantics of the multi-threaded
program.

- Karthik.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

The Semantics of 'volatile' 73
volatile 4
Qsort() messing with my entire Code 0
Casting volatile and const variables 3
volatile and multiple threads 10
volatile Info 56
Volatile variable 1
No way to add volatile access? 23

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top