volatile?

R

Rick

Hi,

What exactly is the volatile keyword for in C? and when should we use
it/ avoid it? Thanks

Rick
 
A

Allin Cottrell

Rick said:
What exactly is the volatile keyword for in C? and when should we use
it/ avoid it?

The 'volatile' keyword tells the C compiler that a particular
variable can change its value without warning, and that the
compiler should make no assumptions regarding this variable, of the
sort that compilers might be inclined to make when planning
optimizations.

As I understand it, this keyword is most likely to be used in
writing device drivers, and in general should be avoided unless
you know exactly what you are doing.
 
I

Irrwahn Grausewitz

Rick said:
What exactly is the volatile keyword for in C? and when should we use
it/ avoid it? Thanks

This question has been asked and answered here in c.l.c two days ago.
Please refer to the thread starting with message:

(e-mail address removed).

Regards
 
R

Randy Howard

You can find good information about volatile here:

http://www.programmersheaven.com/articles/pathak/article1.htm

Sadly OT for c.l.c:
Except for the part about using it for shared data between threads.
It is an unfortunately com mom misconception that volatile can be
used in threaded code as a synch method. It is necessary to
in such programs to avoid the compiler optimizing out a loop on
a variable that might be modified in another thread. There are
typically better (I.e. more efficient) methods of doing that
in most thread libraries. Nevertheless, using volatile for such
conditions does not obviate the need for (insert favorite locking
mechanism, such as mutex, critical section, ....).

The article does not make that clear, so reader beware that
things as complicated as thread programming typically aren't
explained sufficiently in a single short article. This one
included. :)
 

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


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top