public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Jan Chludzinski <jan.chludzinski@gmail.com>
To: cygwin@cygwin.com, gcc-help@gcc.gnu.org
Subject: Re: Pthread error?
Date: Fri, 29 Jul 2011 17:10:00 -0000	[thread overview]
Message-ID: <CADqjQ-J34-mayfS-g-RneUe9QbzMauHbZauG5Cq2iOjxr2QW4A@mail.gmail.com> (raw)
In-Reply-To: <CADqjQ-JDwMxofjUJ0izOhyAaen61bmmG7UgWXwk-22ifcdF1AA@mail.gmail.com>

Don't know why all the white space in the code turned intro "?".
Hopefully this is better:

#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: 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 <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;
}

On Fri, Jul 29, 2011 at 12:59 PM, Jan Chludzinski
<jan.chludzinski@gmail.com> 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 <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

  parent reply	other threads:[~2011-07-29 17:10 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CADqjQ-J73y4M18LJH9a84VUZ+Pj8JsphW9SX9TDuLgd2YJ0aAg@mail.gmail.com>
2011-07-29 16:59 ` Jan Chludzinski
2011-07-29 17:05   ` Eric Blake
2011-07-29 17:10   ` Jan Chludzinski [this message]
2011-07-29 17:16 ` Jan Chludzinski
2011-07-29 17:29   ` Jan Chludzinski
2011-07-29 20:18     ` Corinna Vinschen
2011-07-30  9:53     ` Jan Chludzinski
2011-07-30 10:23       ` Corinna Vinschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CADqjQ-J34-mayfS-g-RneUe9QbzMauHbZauG5Cq2iOjxr2QW4A@mail.gmail.com \
    --to=jan.chludzinski@gmail.com \
    --cc=cygwin@cygwin.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).