/******************************************************************
 * simple3.c -- multithreaded "hello world" using pt_fork()
 *
 * 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_arg_t *info)
{
  int id=pt_myid(info);

  printf("Hello, world, I'm %d\n",id);
  /* stash our id into our slot */
  *((int *) pt_data(info) + id)=id;
  /* no meaningful return value */
  return(NULL);
}

/******** this is the main thread's code */
int main(int argc, char *argv[])
{
  int i;
  int retvals[NTHREADS];

  /* init retvals */
  for (i=0; i<NTHREADS; i++) retvals[i]=-1;

  /* do it */
  pt_fork(NTHREADS,    /* # of threads to create                */
          hola,        /* routine they execute                  */
          retvals,     /* a piece of global data they all share */
          NULL);       /* ignore exit codes                     */

  /* check return values */
  for (i=0; i<NTHREADS; i++)
    if (retvals[i]!=i) {
      fprintf(stderr,"thread %d didn't return a value\n",i);
      exit(1);
    }
  return(0);
}

/* EOF simple3.c */

