Re: confused at code generation for a empty loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux