From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26011 invoked by alias); 30 Nov 2007 06:46:16 -0000 Received: (qmail 26000 invoked by uid 22791); 30 Nov 2007 06:46:15 -0000 X-Spam-Check-By: sourceware.org Received: from wa-out-1112.google.com (HELO wa-out-1112.google.com) (209.85.146.176) by sourceware.org (qpsmtpd/0.31) with ESMTP; Fri, 30 Nov 2007 06:46:08 +0000 Received: by wa-out-1112.google.com with SMTP id m16so2486606waf for ; Thu, 29 Nov 2007 22:46:06 -0800 (PST) Received: by 10.115.49.16 with SMTP id b16mr1360354wak.1196405162241; Thu, 29 Nov 2007 22:46:02 -0800 (PST) Received: from ghost ( [221.218.185.158]) by mx.google.com with ESMTPS id l23sm57781waf.2007.11.29.22.45.52 (version=SSLv3 cipher=OTHER); Thu, 29 Nov 2007 22:45:59 -0800 (PST) Date: Fri, 30 Nov 2007 07:16:00 -0000 From: "PRC" To: gcc-help Subject: confused at code generation for a empty loop Message-ID: <200711301445245306580@gmail.com> X-mailer: Foxmail 6, 9, 201, 16 [cn] Mime-Version: 1.0 Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: 7bit Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2007-11/txt/msg00432.txt.bz2 Here is my code: ================================================================================= #include #include #define NUM_THREADS 1 static int flag = 0; void *thread(void *threadid) { int ret = rand(); printf("thread return %d!\n", ret); sleep(10); flag = 1; return (void *)ret; } int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc; rc = pthread_create(&threads[0], NULL, thread, (void *)0); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } while( flag == 0 ) ; return 0; } ================================================================================= gcc -O2 -g -lpthread a.c objdump -S a.out > a.S `cat a.S` shows: ================================================================================= if (rc){ 80484d4: 85 c0 test %eax,%eax 80484d6: 75 15 jne 80484ed 80484d8: a1 68 97 04 08 mov 0x8049768,%eax 80484dd: 85 c0 test %eax,%eax 80484df: 90 nop printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } while( flag == 0 ) ; 80484e0: 74 fe je 80484e0 ================================================================================ You can see the variable `flag` is read only once. If the value of `flag` is 0 at the first time, the program will trap into a dead loop and never exit. And gcc knows the value of `flag` could be modified in the routine `thread`. But if I modify the line "while( flag == 0 ) ;" to "while(flag == 0) printf("waiting..\n");" and recompile the source code, the output assembly code becomes: ================================================================================= while( flag == 0 ) printf("waiting..\n"); 8048510: c7 04 24 74 86 04 08 movl $0x8048674,(%esp) 8048517: e8 90 fe ff ff call 80483ac 804851c: a1 b8 97 04 08 mov 0x80497b8,%eax 8048521: 85 c0 test %eax,%eax 8048523: 74 eb je 8048510 ================================================================================= `flag` is read in each loop. Then when the value of `flag` is modified, the loop terminates. I wonder why gcc generates code for a empty loop like that. Is it a bug or for optimization in some case? My gcc is: Using built-in specs. Target: i386-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux Thread model: posix gcc version 4.1.1 20060525 (Red Hat 4.1.1-1)