/* This example is copied from */ /* #include #define MAX 10 #define MAX_COUNT 15 void * increment(int *id); void * watch(int *id); int count =0; pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t count_max = PTHREAD_COND_INITIALIZER; int thread_id[3] = {0,1,2}; main() { int i; /* define the type to be pthread */ pthread_t thread[3]; /* create 3 threads*/ pthread_create(&thread[2], NULL, (void *)watch, &thread_id[2]); pthread_create(&thread[0], NULL, (void *)increment, &thread_id[0]); pthread_create(&thread[1], NULL, (void *)increment, &thread_id[1]); for(i=0; i< 3 ; i++) { pthread_join(thread[i], NULL); } } void * watch(int *id) { /* lock the variable */ pthread_mutex_lock(&count_mutex); while(count <= MAX_COUNT) { /* using the condition variable for waiting for the event */ pthread_cond_wait(&count_max, &count_mutex); printf("Inside the watch() and the value is %d\n", count); fflush(stdout); } /*unlock the variable*/ pthread_mutex_unlock(&count_mutex); } void * increment(int *id) { int i; for(i=0; i< MAX ; i++) { /* lock the variable */ pthread_mutex_lock(&count_mutex); count++; printf("in increment counter by threadof id :%d, and count: %d\n",*id, count); fflush(stdout); /* for the condition notify the thread */ pthread_cond_signal(&count_max); /*unlock the variable*/ pthread_mutex_unlock(&count_mutex); sleep(rand()%2); } }