X-Recipient: archive-cygwin AT delorie DOT com X-SWARE-Spam-Status: No, hits=-1.8 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 MIME-Version: 1.0 In-Reply-To: References: Date: Fri, 29 Jul 2011 13:09:47 -0400 Message-ID: Subject: Re: Pthread error? From: Jan Chludzinski To: cygwin AT cygwin DOT com, gcc-help AT gcc DOT gnu DOT org Content-Type: text/plain; charset=ISO-8859-1 Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id p6THACUT011906 Don't know why all the white space in the code turned intro "?". Hopefully this is better: #include #include #include #include #define checkResults(string, val) { \ if (val) { \ printf("Failed with %d at %s", val, string); \ exit(1); \ } \ } #define NUMTHREADS 5 pthread_once_t oneTimeInit = PTHREAD_ONCE_INIT; int initialized = 0; int resource = 0; void initFunction(void) { printf("Thread %.8x %.8x: INITIALIZE RESOURCE\n"); initialized = 1; resource = 42; printf("Thread %.8x %.8x: initialized is>>> %d\n", pthread_self(), initialized); } void *theThread(void *parm) { int rc; printf("Thread %.8x %.8x: Entered\n", pthread_self()); //if (!initialized) { rc = pthread_once(&oneTimeInit, initFunction); checkResults("pthread_once()\n", rc); //} printf("Thread %.8x %.8x: The resource is %d\n", pthread_self(), resource); return NULL; } int main(int argc, char **argv) { pthread_t thread[NUMTHREADS]; int rc=0; int i; printf("Enter Testcase - %s\n", argv[0]); printf("Create/start threads\n"); for (i=0; i wrote: > > The code below appears to have incorrect behavior.  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.  The really strange thing is the printf(...) in > initFunction().  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) {             \ >  if (val) {                                     \ >    printf("Failed with %d at %s", val, string); \ >    exit(1);                                     \ >  }                                              \ > } > > #define                 NUMTHREADS   5 > pthread_once_t          oneTimeInit = PTHREAD_ONCE_INIT; > int                     initialized = 0; > int                     resource    = 0; > > void initFunction(void) > { >    printf("Thread %.8x %.8x: INITIALIZE RESOURCE\n"); >    initialized = 1; >    resource = 42; >    printf("Thread %.8x %.8x: resource is>>> %d\n", >      pthread_self(), resource); > } > > void *theThread(void *parm) > { >   int   rc; >   printf("Thread %.8x %.8x: Entered\n", pthread_self()); >   //if (!initialized) { >     rc = pthread_once(&oneTimeInit, initFunction); >     checkResults("pthread_once()\n", rc); >     //} >   printf("Thread %.8x %.8x: The resource is %d\n", >      pthread_self(), resource); >   return NULL; > } > > int main(int argc, char **argv) > { >   pthread_t             thread[NUMTHREADS]; >   int                   rc=0; >   int                   i; > >   printf("Enter Testcase - %s\n", argv[0]); > >   printf("Create/start threads\n"); >   for (i=0; i     rc = pthread_create(&thread[i], NULL, theThread, NULL); >     checkResults("pthread_create()\n", rc); >   } > >   printf("Wait for the threads to complete, and release their resources\n"); >   for (i=0; i     rc = pthread_join(thread[i], NULL); >     checkResults("pthread_join()\n", rc); >   } > >   printf("Main completed\n"); >   return 0; > } -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple