/* * slowProdCon.c++ Producer-Consumer Problem * one is slower than the other * $ g++ -Wall slowProdCon.c++ -lpthread */ #include using namespace std; #include #include #include "queuePth1.h" #define DELAY 10000 int data; int dataCount=0; void *producer(void *); void *consumer(void *); pthread_mutex_t makeAtomic = PTHREAD_MUTEX_INITIALIZER; int main(int count, char *vect[]) { pthread_t thID1, thID2; // thread ID int num; cout << "Enter number of data: "; cin >> num ; pthread_create(&thID1, NULL, producer, (void *)&num); pthread_create(&thID2, NULL, consumer, (void *)&num); pthread_join(thID1, NULL); pthread_join(thID2, NULL); return 0; } void *producer(void *par){ int num = *(int *)par; for(int i=1; i<=num; ) { pthread_mutex_lock(&makeAtomic); cout << "P-> "; if(dataCount==0){ dataCount = 1; data=i; ++i; } pthread_mutex_unlock(&makeAtomic); for(int j=0; j