#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>

#define NUMINCR 100000
#define NUMTHREADS 3
#define YIELDPROB 0.001

int yield_thread(double prob);

static int counter = 0;

/* ARGSUSED */
static void *count0(void *arg) {
   int i;
   for (i=0;i<NUMINCR;i++) {
      counter++;
   }
   return NULL;
}

int main(void) {
   int error;
   int i;
   pthread_t tid[NUMTHREADS];

   fprintf(stderr,"This program was written by S. Robbins\n");
   fprintf(stderr,"Number of threads: %d, increments per thread: %d\n",
                   NUMTHREADS,NUMINCR);
   for (i=0;i<NUMTHREADS;i++) {
      if (error = pthread_create(tid+i, NULL, count0, NULL)) {
         fprintf(stderr, "Failed to create thread %d: %s\n",i,strerror(error));
         return 1;
      }
   }
   for (i=0;i<NUMTHREADS;i++) {
      if (error = pthread_join(tid[i], NULL)) {
         fprintf(stderr, "Failed to join thread 1: %s\n", strerror(error));
         return 1;
      }
   }

   fprintf(stderr,"Final value of counter should be %d\n",NUMINCR*NUMTHREADS);
   fprintf(stderr,"Final value of counter is        %d\n",counter);
   return 0;
}

