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

Volatile variables in C!!

Can anybody please tell me the complete properties if volatile
variables in C. And what are specific uses of them?
Thanks in advance.
Nov 14 '05 #1
7 3194

"r_o_h_i_t" <r_*********@yahoo.co.in> wrote in message
news:7c*************************@posting.google.co m...
Can anybody please tell me the complete properties if volatile
variables in C. And what are specific uses of them?
Thanks in advance.


Do.

Your.

Own.

Homework.

Tom
Nov 14 '05 #2
r_o_h_i_t wrote:
Can anybody please tell me the complete properties if volatile
variables in C.
This is the important one:

"An object that has volatile-qualified type may be modified in ways unknown
to the implementation or have other unknown side effects." (Taken from C99,
6.7.3(6))
And what are specific uses of them?


One fairly typical place in which you might need to use a volatile object
would be where you need to point to, say, a system clock. Imagine, for
example, an embedded system which has a particular 32-bits-wide memory
location containing the time, using 5 bits for the hours, 6 for the
minutes, 6 for the seconds, and - um - 15 for the 32768ths of a second. :-)
The only support the implementation provides for the clock is a function
returning its address. Thus:

volatile unsigned long *sysclock = _GetSystemClockAddress();

Now, your code might have something to do which will only take a few
microseconds, but it wants to wait until a tick has just happened, so that
it can do <whatever> without having the clock change under it. It could do
something like this:

while(*sysclock == *sysclock)
{
continue;
}
/* the clock just ticked */

If sysclock were not volatile-qualified, the compiler would almost certainly
treat this as a forever-loop. Not good! But because it's
volatile-qualified, the compiler can't assume the value of *sysclock won't
change during the evaluation of the condition. So (modulo poor
explanations!) it not only has to read *sysclock every time it goes round
the loop, but it actually has to read it /twice/. The volatile keyword
forces this. Consequently, in this case, when the clock goes "tick", the
loop will end - assuming the processor is fast enough to catch a tick "in
progress" (If it isn't fast enough, we might just have a forever loop after
all. Embedded programming is harder than it looks.)

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #3
In article <7Q******************@twister01.bloor.is.net.cable .rogers.com>,
to*@securescience.net says...

"r_o_h_i_t" <r_*********@yahoo.co.in> wrote in message
news:7c*************************@posting.google.co m...
Can anybody please tell me the complete properties if volatile
variables in C. And what are specific uses of them?
Thanks in advance.


Do.

Your.

Own.

Homework.

Tom


First of all, not everyone is still in college. Second, volatile
is probably one of the most understood and misapplied keywords
in the language. Discussing it here isn't a bad thing.

Richard gave a good short treatise on some of the uses of it.

Slightly OT: Anyone that has spent much time reading
comp.programming.threads has seen thousands of words on the topic,
most of which are based upon *false* assumptions about volatile being
a safe replacement for mutexes or other more expensive locking
primitives.

In a sense, volatile is a useful, standardized way of handling non-
standard platform specific extensions. :-)

H&S 5th Ed covers this in more than the usual amount of detail in
section 4.4.5.

K&R2 says that declaring an object volatile "announces that it
has special properties relevant to optimization". It also talks
separately about its use for memory-mapped I/O to device registers,
which I think proves it's usefulness for platform-specific code.

--
Randy Howard
2reply remove FOOBAR

Nov 14 '05 #4
Richard Heathfield wrote:
.... snip ...
while(*sysclock == *sysclock)
{
continue;
}
/* the clock just ticked */

If sysclock were not volatile-qualified, the compiler would
almost certainly treat this as a forever-loop. Not good! But
because it's volatile-qualified, the compiler can't assume the
value of *sysclock won't change during the evaluation of the

.... snip ...

I am not enamored of your code, volatile or not. With no
constraints on the execution time of the continue statement, I
fear many ticks are going to be missed. I suggest:

whatever t1, t2;
....
while (t2 == (t1 = *sysclock)) t2 = t1;

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5
CBFalconer wrote:
With no
constraints on the execution time of the continue statement, I
fear many ticks are going to be missed. I suggest:

whatever t1, t2;
....
while (t2 == (t1 = *sysclock)) t2 = t1;


Thanks for the correction.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #6
In article <c0**********@titan.btinternet.com>,
Richard Heathfield <do******@address.co.uk.invalid> wrote:
Imagine, for
example, an embedded system which has a particular 32-bits-wide memory
location containing the time, using 5 bits for the hours, 6 for the
minutes, 6 for the seconds, and - um - 15 for the 32768ths of a second. :-)
The only support the implementation provides for the clock is a function
returning its address. Thus:

volatile unsigned long *sysclock = _GetSystemClockAddress();

Now, your code might have something to do which will only take a few
microseconds, but it wants to wait until a tick has just happened, so that
it can do <whatever> without having the clock change under it. It could do
something like this:

while(*sysclock == *sysclock)
{
continue;
}
/* the clock just ticked */


*sysclock could change during the (virtual) continue part
of the loop. It is likely the wait will often be longer
than 1/32768 s. The loop could even be infinite. Use

{
unsigned long t0;
for (t0 = *sysclock; t0==*sysclock; );
}
Francois Grieu
Nov 14 '05 #7
Richard Heathfield wrote:
CBFalconer wrote:
With no
constraints on the execution time of the continue statement, I
fear many ticks are going to be missed. I suggest:

whatever t1, t2;
....
while (t2 == (t1 = *sysclock)) t2 = t1;


Thanks for the correction.


Actually, it was wrong. Try:

while (t2 == (t1 = *sysclock)) continue;
t2 = t1;

and there is still a possible glitch on the first use.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Radde | last post by:
HI, Can volatile variables be accessed by many processess..or only one process can access it.. Cheers..
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...
22
by: Assaf | last post by:
hi all i know that i should not cross-post, but i am not sure to which group to post this question. 2 quesions about volatile: 1. i use volatile when 2 threads access the same variable...
6
by: Clausfor | last post by:
Hello, I have a problem with restoring variables in the setjmp/longjmp functions: K&R2 for longjmp says: "Accessible objects have the same value they had when longjmp was called, except for...
6
by: titan nyquist | last post by:
Can you make volatile structures in C#? I have a static class, to have "global" variables. This allows the whole program to see them. I make them "volatile" to avoid multi- threading accessing...
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...
10
by: S James S Stapleton | last post by:
Is volatile necessary anymore? I have a two-thread piece of code I've been testing to figure out what volatile does (fairly simple code, uses pthreads). I have an update thread (variables passed as...
16
by: Ark Khasin | last post by:
I have a memory-mapped peripheral with a mapping like, say, struct T {uint8_t read, write, status, forkicks;}; If I slap a volatile on an object of type struct T, does it guarantee that all...
3
by: C++Liliput | last post by:
It seems that the keyword "volatile" is used to make sure that threads reading (or writing to) the same data should see a consistent picture of the variable i.e. updates made to the common data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.