This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Pthread error?


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]