delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2011/07/29/13:01:21

X-Recipient: archive-cygwin AT delorie DOT com
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
MIME-Version: 1.0
In-Reply-To: <CADqjQ-J73y4M18LJH9a84VUZ+Pj8JsphW9SX9TDuLgd2YJ0aAg@mail.gmail.com>
References: <CADqjQ-J73y4M18LJH9a84VUZ+Pj8JsphW9SX9TDuLgd2YJ0aAg AT mail DOT gmail DOT com>
Date: Fri, 29 Jul 2011 12:59:23 -0400
Message-ID: <CADqjQ-JDwMxofjUJ0izOhyAaen61bmmG7UgWXwk-22ifcdF1AA@mail.gmail.com>
Subject: Pthread error?
From: Jan Chludzinski <jan DOT chludzinski AT gmail DOT com>
To: cygwin AT cygwin DOT com, gcc-help AT gcc DOT gnu DOT org
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id p6TH1HEB011296

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 <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#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 <NUMTHREADS; ++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 <NUMTHREADS; ++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


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019