Behavior of pthreads

Hi All,
I ve written a small program to get started off with pthreads. I somehow feel the program doesnt meet the purpose. Please find the code and the output below. Please find my question at the bottom.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

void *PrintThread1(void *threadid)
{
  int i;
  for (i=0;i<50;i++)
  {
    printf("\n%d: Thread1!\n", threadid);
  }
}
void *PrintThread2(void *threadid)
{
    int i;
    for (i=0;i<50;i++)
    printf("\n%d: Thread2!\n", threadid);
    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
  pthread_t threads, threads1;
  int rc, t1=0, t2=1;

  printf("Creating thread %d\n", t1);
  rc = pthread_create(&threads, NULL, PrintThread1, (void *)t1);
    if (rc)
    {
      printf("ERROR; return code from pthread_create() is %d\n", rc);
    exit(-1);
    }
  printf("Creating thread %d\n", t2);
  rc = pthread_create(&threads1, NULL, PrintThread2, (void *)t2);
    if (rc)
    {
      printf("ERROR; return code from pthread_create() is %d\n", rc);
    exit(-1);
    }

  pthread_exit(NULL);
}

Output
--------
0: Thread1! (printed when i=0)

0: Thread1!

...............

...............

0: Thread1! (printed when i=49)

1: Thread2! (printed when i=0)

1: Thread2!

-----------

-----------

1: Thread2! (printed when i=49)

My question is:
If both the threads are running the output from the functions printThread1 and PrintThread2 should be mingled?
i am in HP-UX 10.20
Same program in cygwin environment yields mingled output.
Please help me with this...

raj

Simple answer - do not expect the same behavior for this kind of application across hardware and OS. 10.20 is an older OS, so it may schedule threads as lightweight processes, like earlier versions of Linux.

I don't know this to be true, I'm just making a suggestion.

You can't guarantee which thread runs first unless you set mutexes to control "who" can run.

Why do you want a certain order?

Are we comparing the performance of a threaded program on a uniprocessor to the performance of the same program on a SMP (or ccNUMA) box. I hope that both boxes have the same number of cpu's to help even things out a little bit.

I just tried the program on HP-UX and I'm not getting any reproducable results. There does seem to be a trend that creating a thread takes a while if a new kernel thread must be allocated for it. This may give the first thread enough time to run to competion in a second cpu while the first cpu is still building the 2nd thread. Lengthening the loop counter helped. So did immediately rerunning the program. Anyway, right now, I can't get back to non-intermingled mode.

I tried it on HPUX 11.0 - a vclass 12 cpu box and a kclass 6 cpu box.

I don't get any perceivable pattern except that at each end of the stream we get
monotony.

I second Perderabo - you can't expect this to behave "predictably", and threads on SMP vs threads on single cpu box is not a fair comparison.

I dont need a specific order for the output. I was wondering whether threads are created at all.
Thank you all for the inputs

anyways, i think its time to upgrade the OS :slight_smile: