This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: confused at code generation for a empty loop


On Friday 30 November 2007 07:45, PRC wrote:

> static int flag = 0;
[thread writes to flag]

[meanwhile in parent thread]
>    while( flag == 0 ) ;

There's no way for gcc to tell, that your thread will change "flag", so gcc 
assumes it stays the same. gcc doesn't know what pthread_create does.

> But if I modify the line "while( flag == 0 ) ;" to "while(flag == 0)
> printf("waiting..\n");"

Now gcc has no way to tell, that "printf" will *not* change "flag" somehow, so 
gcc rereads "flag" 

Add a "volatile" modifier to "flag" to inform gcc that "flag" may change at 
any moment.



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]