public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Pthread error?
       [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
  2011-07-29 17:16 ` Jan Chludzinski
  1 sibling, 2 replies; 8+ messages in thread
From: Jan Chludzinski @ 2011-07-29 16:59 UTC (permalink / raw)
  To: cygwin, gcc-help

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Pthread error?
  2011-07-29 16:59 ` Pthread error? Jan Chludzinski
@ 2011-07-29 17:05   ` Eric Blake
  2011-07-29 17:10   ` Jan Chludzinski
  1 sibling, 0 replies; 8+ messages in thread
From: Eric Blake @ 2011-07-29 17:05 UTC (permalink / raw)
  To: cygwin

On 07/29/2011 10:59 AM, Jan Chludzinski wrote:
> The code below appears to have incorrect behavior.  The output is:

Compile with -Wall.

> Thread 00a104f8 0000002a: The resource is 0

>     printf("Thread %.8x %.8x: resource is>>>  %d\n",
>       pthread_self(), resource);

Three uses of %, but only two arguments.  You're reading random stuff 
off the stack because you used printf incorrectly.

-- 
Eric Blake   eblake@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

--
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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Pthread error?
  2011-07-29 16:59 ` Pthread error? Jan Chludzinski
  2011-07-29 17:05   ` Eric Blake
@ 2011-07-29 17:10   ` Jan Chludzinski
  1 sibling, 0 replies; 8+ messages in thread
From: Jan Chludzinski @ 2011-07-29 17:10 UTC (permalink / raw)
  To: cygwin, gcc-help

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Pthread error?
       [not found] <CADqjQ-J73y4M18LJH9a84VUZ+Pj8JsphW9SX9TDuLgd2YJ0aAg@mail.gmail.com>
  2011-07-29 16:59 ` Pthread error? Jan Chludzinski
@ 2011-07-29 17:16 ` Jan Chludzinski
  2011-07-29 17:29   ` Jan Chludzinski
  1 sibling, 1 reply; 8+ messages in thread
From: Jan Chludzinski @ 2011-07-29 17:16 UTC (permalink / raw)
  To: cygwin, gcc-help

Thanks!

This is an example (from IBM) I cut-and-paste into Emacs to better
understand pthread_once(...).  Didn't notice the two "%.8x" in
printf().

Thanks again, Jan

--
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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Pthread error?
  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
  0 siblings, 2 replies; 8+ messages in thread
From: Jan Chludzinski @ 2011-07-29 17:29 UTC (permalink / raw)
  To: cygwin, gcc-help

Can't blame IBM either.  I had to replace pthread_getthreadid_np() (in
the IBM example code) with pthread_self() because Cygwin doesn't
support/have pthread_getthreadid_np().

And the IBM docs say pthread_getthreadid_np() returns "a structure
containing the hi and low order 4 bytes of the 64bit ID".  The 2
"%.8x" would make sense.

---Jan


On Fri, Jul 29, 2011 at 1:16 PM, Jan Chludzinski
<jan.chludzinski@gmail.com> wrote:
> Thanks!
>
> This is an example (from IBM) I cut-and-paste into Emacs to better
> understand pthread_once(...).  Didn't notice the two "%.8x" in
> printf().
>
> Thanks again, Jan
>

--
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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Pthread error?
  2011-07-29 17:29   ` Jan Chludzinski
@ 2011-07-29 20:18     ` Corinna Vinschen
  2011-07-30  9:53     ` Jan Chludzinski
  1 sibling, 0 replies; 8+ messages in thread
From: Corinna Vinschen @ 2011-07-29 20:18 UTC (permalink / raw)
  To: cygwin

On Jul 29 13:29, Jan Chludzinski wrote:
> Can't blame IBM either.  I had to replace pthread_getthreadid_np() (in
> the IBM example code) with pthread_self() because Cygwin doesn't
> support/have pthread_getthreadid_np().

pthread_getthreadid_np is a non-standard IBM extension.  You won't find
it in the POSIX specs.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

--
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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Pthread error?
  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
  1 sibling, 1 reply; 8+ messages in thread
From: Jan Chludzinski @ 2011-07-30  9:53 UTC (permalink / raw)
  To: cygwin

> pthread_getthreadid_np is a non-standard IBM extension.  You won't find
> it in the POSIX specs.

Does Cygwin support the full POSIX pthread spec?

---Jan

--
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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Pthread error?
  2011-07-30  9:53     ` Jan Chludzinski
@ 2011-07-30 10:23       ` Corinna Vinschen
  0 siblings, 0 replies; 8+ messages in thread
From: Corinna Vinschen @ 2011-07-30 10:23 UTC (permalink / raw)
  To: cygwin

On Jul 30 05:52, Jan Chludzinski wrote:
> > pthread_getthreadid_np is a non-standard IBM extension.  You won't find
> > it in the POSIX specs.
> 
> Does Cygwin support the full POSIX pthread spec?

That would be nice, but, no, not yet.  See
http://cygwin.com/cygwin-api/compatibility.html

In the next Cygwin version 1.7.10, most of the missing pthread functions
from http://cygwin.com/cygwin-api/std-notimpl.html will have been added.
Still missing for 1.7.10 since nobody implemented them yet are

    pthread_barrier[...]
    pthread_mutexattr_getrobust
    pthread_mutexattr_setrobust
    pthread_mutex_consistent
    pthread_mutex_timedlock
    pthread_rwlock_timedrdlock
    pthread_rwlock_timedwrlock

In theory, except for the barrier functions, they shouldn't be very hard
to implement.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

--
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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2011-07-30 10:23 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CADqjQ-J73y4M18LJH9a84VUZ+Pj8JsphW9SX9TDuLgd2YJ0aAg@mail.gmail.com>
2011-07-29 16:59 ` Pthread error? Jan Chludzinski
2011-07-29 17:05   ` Eric Blake
2011-07-29 17:10   ` Jan Chludzinski
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

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).