Hi, I have one doubt in my mind that why below programme ended up in infinite loop if return NULL statement is commented. pthread_cond_signal will take control out of loop and gracefull exit is done from consumer thread . #include <pthread.h> #include <stdio.h> pthread_mutex_t mutex; pthread_cond_t cond; int buffer[100]; int loops = 5; int length = 0; void *producer(void *arg) { int i=0; for (;;) { pthread_mutex_lock(&mutex); if(i==5) //Max_buffer 5 { pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); return NULL;//if it is commented programme will hang } else { buffer[length++] = i++; printf("producer length %d\n", length); //pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex); } } } void *consumer(void *arg) { int i=0; printf("mohan"); for (;;) { pthread_mutex_lock(&mutex); if(length == 0) { printf(" consumer waiting...\n"); pthread_cond_wait(&cond, &mutex); } int item = buffer[--length]; printf("Consumer %d\n", item); pthread_mutex_unlock(&mutex); if(item==0) return NULL; } } int main(int argc, char *argv[]) { pthread_mutex_init(&mutex, 0); pthread_cond_init(&cond, 0); pthread_t pThread, cThread; pthread_create(&cThread, 0, consumer, 0); pthread_create(&pThread, 0, producer, 0); pthread_join(cThread, NULL); pthread_join(pThread, NULL); pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond); return 0; }

Please post your code using the </>Code option in the editor. It is basically unreadable as it is, unless you really want to enter in the "Obfuscated C Contest"... :-)