/**************************************************************
 * Programming with pthread: pthreadProg1.c  
 * Race condition
 * Compile: $ cc -Wall -lpthread pthreadProg1.c 
 *          $ ./a.out 500000
 * ************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int times, n = 0 ;
void * thread1(void *) ;
void * thread2(void *) ;
void inc() {int a; a = n; a = a+1; n = a;}
void dec() {int a; a = n; a = a-1; n = a;}

int main(int count, char *vect[]) { // argument is data for times
     pthread_t thID1, thID2; 

     if(count < 2) {
          printf("No argument for times\n") ;
          exit(0) ;
     }     
     times = atoi(vect[1]) ;
     pthread_create(&thID1, NULL, thread1, NULL) ;
     pthread_create(&thID2, NULL, thread2, NULL) ;
     pthread_join(thID1, NULL) ;     
     pthread_join(thID2, NULL) ;

     printf("n: %d\n", n) ;     
     return 0 ;
}

void *thread1(void *vp) {
     int i ;

     for(i=1; i<times; ++i) inc() ; 
     return NULL ;
}

void *thread2(void *vp) {
     int i ;

     for(i=1; i<times; ++i) dec() ; 
     return NULL ;
}

