/******************************************************************
 * simple2.c -- multithreaded "hello world" using pt_xxx macros
 *
 * Author: Mark Hays <hays@math.arizona.edu>
 */

#include "pt.h"

#include <stdio.h>

#define NTHREADS 4

/******** this is the thread code */
pt_addr_t hola(pt_addr_t arg)
{
  printf("Hello, world, I'm %d\n",*(int *) arg);
  return arg;
}

/******** this is the main thread's code */
int main(int argc,char *argv[])
{
  int worker;
  pthread_t threads[NTHREADS];                /* holds thread info */
  int ids[NTHREADS];                          /* holds thread id   */
  int *status;                                /* holds return code */

  /* create the threads */
  for (worker=0; worker<NTHREADS; worker++) {
    ids[worker]=worker;
    pt_create(&threads[worker],               /* thread struct     */
              hola,                           /* start routine     */
              &ids[worker],                   /* arg to routine    */
              "pt_create");                   /* error message     */
  }
  /* reap the threads as they exit */
  for (worker=0; worker<NTHREADS; worker++) {

    /* wait for thread to terminate */
    pt_wait(&threads[worker],(pt_addr_t) &status,"pt_wait");

    /* check thread's exit status */
    if (*status != worker) {
      fprintf(stderr,"thread %d terminated abnormally\n",worker);
      exit(1);
    }
  }
  return(0);
}

/* EOF simple2.c */

