From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23636 invoked by alias); 29 Jul 2011 16:59:51 -0000 Received: (qmail 23613 invoked by uid 22791); 29 Jul 2011 16:59:48 -0000 X-SWARE-Spam-Status: No, hits=-1.7 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: sourceware.org Received: from mail-ww0-f45.google.com (HELO mail-ww0-f45.google.com) (74.125.82.45) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 29 Jul 2011 16:59:25 +0000 Received: by wwj40 with SMTP id 40so3191475wwj.2 for ; Fri, 29 Jul 2011 09:59:24 -0700 (PDT) MIME-Version: 1.0 Received: by 10.227.207.207 with SMTP id fz15mr2032632wbb.76.1311958764022; Fri, 29 Jul 2011 09:59:24 -0700 (PDT) Received: by 10.227.29.144 with HTTP; Fri, 29 Jul 2011 09:59:23 -0700 (PDT) In-Reply-To: References: Date: Fri, 29 Jul 2011 16:59:00 -0000 Message-ID: Subject: Pthread error? From: Jan Chludzinski To: cygwin@cygwin.com, gcc-help@gcc.gnu.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com X-SW-Source: 2011-07/txt/msg00380.txt.bz2 The code below appears to have incorrect behavior.=A0 The output is: $ ./a.exe Enter Testcase - ./a Create/start threads Thread 009e0290 00000000: Entered Thread 009f0320 00000000: Entered Thread 009f03a8 00000000: Entered Thread 18dbce64 00000000: INITIALIZE RESOURCE Wait for the threads to complete, and release their resources Thread 009f0430 00000000: Entered Thread 00a104f8 00000000: Entered Thread 009e0290 00000001: resource is>>> 0 Thread 009e0290 0000002a: The resource is 0 Thread 009f0320 0000002a: The resource is 0 Thread 009f03a8 0000002a: The resource is 0 Thread 009f0430 0000002a: The resource is 0 Thread 00a104f8 0000002a: The resource is 0 Main completed If I understand pthread_once(...) correctly, the output should be: Thread ... ...: The resource is 42 for all threads.=A0 The really strange thing is the printf(...) in initFunction().=A0 This should print "resource is>>> 42" but I get: "resource is>>> 0". What's up? I'm using Cygwin 1.7 on Windows 7 with gcc (GCC) 4.3.4 20090804 (release) 1. ---John ///////////////////////////////////////////////////////////////////////////= ///////////////////////////// #include #include #include #include #define checkResults(string, val) {=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 \ =A0if (val) {=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 \ =A0=A0 printf("Failed with %d at %s", val, string); \ =A0=A0 exit(1);=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 \ =A0}=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 \ } #define=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 NUMTHREADS=A0=A0 5 pthread_once_t=A0=A0=A0=A0=A0=A0=A0=A0=A0 oneTimeInit =3D PTHREAD_ONCE_INIT; int=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 initialized= =3D 0; int=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 resource=A0= =A0=A0 =3D 0; void initFunction(void) { =A0=A0 printf("Thread %.8x %.8x: INITIALIZE RESOURCE\n"); =A0=A0 initialized =3D 1; =A0=A0 resource =3D 42; =A0=A0 printf("Thread %.8x %.8x: resource is>>> %d\n", =A0=A0=A0 =A0pthread_self(), resource); } void *theThread(void *parm) { =A0 int=A0=A0 rc; =A0 printf("Thread %.8x %.8x: Entered\n", pthread_self()); =A0 //if (!initialized) { =A0=A0=A0 rc =3D pthread_once(&oneTimeInit, initFunction); =A0=A0=A0 checkResults("pthread_once()\n", rc); =A0=A0=A0 //} =A0 printf("Thread %.8x %.8x: The resource is %d\n", =A0=A0=A0 =A0pthread_self(), resource); =A0 return NULL; } int main(int argc, char **argv) { =A0 pthread_t=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 thread[NUMTHREADS]; =A0 int=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 rc=3D0; =A0 int=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 i; =A0 printf("Enter Testcase - %s\n", argv[0]); =A0 printf("Create/start threads\n"); =A0 for (i=3D0; i