This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: confused at code generation for a empty loop
- From: Marco Manfredini <mldb at gmx dot net>
- To: gcc-help at gcc dot gnu dot org
- Date: Fri, 30 Nov 2007 11:58:39 +0100
- Subject: Re: confused at code generation for a empty loop
- References: <200711301445245306580@gmail.com>
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.