/******************************************************************************
* FILE: hello32.c
* DESCRIPTION:
*   A "hello world" Pthreads program which creates a large number of 
*   threads per process.  A sleep() call is used to insure that all
*   threads are in existence at the same time.  Each Hello thread does some
*   work to demonstrate how the OS scheduler behavior affects thread 
*   completion order.
* AUTHOR: Blaise Barney
* LAST REVISED: 01/29/09
******************************************************************************/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef _CIVL
#include <unistd.h>
#include <math.h>
#include <civlc.cvh>

$input int NTHREADS;
$input int NSTEPS;
#else
#define NTHREADS	32
#endif

void *Hello(void *threadid)
{
   int i;
   double result=0.0;
   sleep(3);
#ifdef _CIVL
    for (i=0; i<NSTEPS; i++) {
#else
   for (i=0; i<10000; i++) {
#endif
     result = result + sin(i) * tan(i);
     }
   printf("%ld: Hello World!\n", (int)threadid);
   pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
pthread_t threads[NTHREADS];
int rc; 
long t;
for(t=0;t<NTHREADS;t++){
  rc = pthread_create(&threads[t], NULL, Hello, (void *)t);
#ifdef _CIVL
    $assert(!rc);
#endif
  if (rc){
    printf("ERROR: return code from pthread_create() is %d\n", rc);
#ifdef _CIVL
      printf("Code %d\n",rc);
#else
    printf("Code %d= %s\n",rc,strerror(rc));
#endif
      exit(-1);
    }
   }
printf("main(): Created %ld threads.\n", t);
pthread_exit(NULL);
}
