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

question related to compiler optimization

Guys,

Please have a look at the following piece of code:

int gcount = 0;
int gdelay = 0;

/* both gcount and gdelay are changed outside this function by an
interrupt handler code */

somefunc()
{
int count = gcount;
while (count == gcount || gdelay != 0) {
/*some handling */
}

}

I am quite sure that "gcount" should be declared volatile so that
compiler doesn't optimise the condition "count == gcount".
However, I am not sure if "gdelay" should also be declared to be
volatile ?
Nov 28 '07 #1
5 1297
ju**********@yahoo.co.in wrote:
Guys,

Please have a look at the following piece of code:

int gcount = 0;
int gdelay = 0;

/* both gcount and gdelay are changed outside this function by an
interrupt handler code */

somefunc()
{
int count = gcount;
while (count == gcount || gdelay != 0) {
/*some handling */
}

}

I am quite sure that "gcount" should be declared volatile so that
compiler doesn't optimise the condition "count == gcount".
However, I am not sure if "gdelay" should also be declared to be
volatile ?
Any variable that can be changed in another context should be declared
extern. The compiler could see that gdelay is initialised to 0 and
optimise away the expression...

--
Ian Collins.
Nov 28 '07 #2
On Nov 27, 10:43 pm, "junky_fel...@yahoo.co.in"
<junky_fel...@yahoo.co.inwrote:
Guys,

Please have a look at the following piece of code:

int gcount = 0;
int gdelay = 0;

/* both gcount and gdelay are changed outside this function by an
interrupt handler code */

somefunc()
{
int count = gcount;
while (count == gcount || gdelay != 0) {
/*some handling */
}

}

I am quite sure that "gcount" should be declared volatile so that
compiler doesn't optimise the condition "count == gcount".
However, I am not sure if "gdelay" should also be declared to be
volatile ?
It does not hurt to declare both as volatile.
Nov 28 '07 #3
Podi wrote:
On Nov 27, 10:43 pm, "junky_fel...@yahoo.co.in"
<junky_fel...@yahoo.co.inwrote:
>Guys,

Please have a look at the following piece of code:

int gcount = 0;
int gdelay = 0;

/* both gcount and gdelay are changed outside this function by an
interrupt handler code */

somefunc()
{
int count = gcount;
while (count == gcount || gdelay != 0) {
/*some handling */
}

}

I am quite sure that "gcount" should be declared volatile so that
compiler doesn't optimise the condition "count == gcount".
However, I am not sure if "gdelay" should also be declared to be
volatile ?

It does not hurt to declare both as volatile.
No, it hurts if you don't!

By way of an example from the OP:

void somefunc()
{
int count = gcount;
while (count == gcount || gdelay != 0) {
count = 42;
}
}

When gcount and gdelay are not volatile, this is one compiler's assembly
output for somefunc:

somefunc:
cmpl $42,gcount
jne .CG3.15

--
Ian Collins.
Nov 28 '07 #4
"Ian Collins" <ia******@hotmail.coma écrit dans le message de news:
5r*************@mid.individual.net...
ju**********@yahoo.co.in wrote:
>Guys,

Please have a look at the following piece of code:

int gcount = 0;
int gdelay = 0;

/* both gcount and gdelay are changed outside this function by an
interrupt handler code */

somefunc()
{
int count = gcount;
while (count == gcount || gdelay != 0) {
/*some handling */
}

}

I am quite sure that "gcount" should be declared volatile so that
compiler doesn't optimise the condition "count == gcount".
However, I am not sure if "gdelay" should also be declared to be
volatile ?

Any variable that can be changed in another context should be declared
extern. The compiler could see that gdelay is initialised to 0 and
optimise away the expression...
nonsense! As long as they are not static, variables defined at global scope
*have* extern linkage.
Furthermore, the interrupt code could be in the same module, for instance in
the form of a signal handler, and these global variables could be made
static then.

Both gcount and gdelay should be declared volatile to account for what the
OP is describing. Incidentally, it may not be sufficient if accessing them
is not guaranteed to be an atomic operation. See sections 7.14 on signal
handling for a discussion of type sig_atomic_t. 7.18.3p3 only guarantees
that type sig_atomic_t support at least 255 different values.

To make matters worse, if your "interrupt" code modifies both gcount and
gdelay, you need further non trivial code to ascertain that the values used
in somefunc() be consistent, because the "interrupt" could occur between the
access to one and the other, or even in the middle of such an access (from
the previous paragraph).

Interrupt handling, and thread programming, is *very* tricky to do right.
Your code may seem to work for a long time before conditions occur that make
it fail. It better not be at the heart of an anti-lock braking system, an
air-bag trigger, or any kind of critical device control unit... This group
is not the best place to discuss these issues, but I recommend that you use
proven techniques to deal with your "interrupt" handling issues.

--
Chqrlie.
Nov 28 '07 #5
Charlie Gordon wrote:
"Ian Collins" <ia******@hotmail.coma écrit dans le message de news:
5r*************@mid.individual.net...
>ju**********@yahoo.co.in wrote:
>>Guys,

Please have a look at the following piece of code:

int gcount = 0;
int gdelay = 0;

/* both gcount and gdelay are changed outside this function by an
interrupt handler code */

somefunc()
{
int count = gcount;
while (count == gcount || gdelay != 0) {
/*some handling */
}

}

I am quite sure that "gcount" should be declared volatile so that
compiler doesn't optimise the condition "count == gcount".
However, I am not sure if "gdelay" should also be declared to be
volatile ?
Any variable that can be changed in another context should be declared
extern. The compiler could see that gdelay is initialised to 0 and
optimise away the expression...

nonsense! As long as they are not static, variables defined at global scope
*have* extern linkage.
Indeed it was, I thought volatile and typed extern....

--
Ian Collins.
Nov 28 '07 #6

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

Similar topics

11
by: Kurt Krueckeberg | last post by:
Given a class X class X { public: X(int); X(const X&); //. . . }; Is this line X x1(1); the same thing as X x2 = 2;
38
by: aaronfude | last post by:
I'm working on a scientific computing application. I need a class called Element which is no more than a collection of integers, or "nodes" and has only on method int getNode(int i). I would...
18
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and...
55
by: ben | last post by:
is it true that a function without an inline keyword never get inlined? If not true when is it inlined or not? ben
83
by: user | last post by:
Hello, Here is the program #include stdio int main(void) { const int num = 100; int *ip;
9
by: Tim Rentsch | last post by:
I have a question about what ANSI C allows/requires in a particular context related to 'volatile'. Consider the following: volatile int x; int x_remainder_arg( int y ){ return x % y; }
5
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
4
by: sagi | last post by:
Hello erveryone,I am a newcomer here and the word of c. Here I have a question confused me a lot that when I read codes I found some declaration like that: "int regcomp(regex_t *restrict...
2
by: sanjay | last post by:
Hi All, I have a doubt in understanding the output of the following program that i executed on my system. I was using DevC++ IDE which uses minGW based compiler. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.