/* a simple producer/consumer using busy-waiting threads

   gcc thisfile.c -lpthread
   a.out numIters

*/
 
#include <pthread.h>
#include <stdio.h>

void *Producer(void *);
void *Consumer(void *);

int produced = 0, consumed = 0;
int data;
int numIters;

/* main() -- read command line and create threads, then
             print result when the threads have quit */

int main(int argc, char *argv[]) {
  /* thread ids and attributes */
  pthread_t pid, cid;  
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);

  numIters = atoi(argv[1]);
  printf("main started\n");
  pthread_create(&pid, &attr, Producer, NULL);
  pthread_create(&cid, &attr, Consumer, NULL);
  pthread_join(pid, NULL);
  pthread_join(cid, NULL);
  printf("main done\n");
}


void *Producer(void *arg) {
  printf("Producer created\n");
  while (produced < numIters) {
    while (produced > consumed) ;    /* wait for buffer to be empty */
    data = produced;
    produced++;
  }
}


void *Consumer(void *arg) {
  int total = 0;
  printf("Consumer created\n");
  while (consumed < numIters) {
    while (produced == consumed) ;   /* wait for buffer to be full */
    total = total+data;
    consumed++;
  }
  printf("for %d iterations, the total is %d\n", numIters, total);
}
