confused at code generation for a empty loop
Marco Manfredini
mldb@gmx.net
Fri Nov 30 10:51:00 GMT 2007
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.
More information about the Gcc-help
mailing list