/* prodConC11.c++ Producer-Consumer Problem on pthread $ g++ -Wall -std=c++11 prodConC11.c++ queueC11.o -lpthread */ #include #include using namespace std; #include "queueC11.h" void producer(queue *); void consumer(queue *); int countP = 0, countC = 0; int main(int count, char *vect[]) { queue *qP; qP = new queue; thread th1(&producer, qP); thread th2(&consumer, qP); th1.join(); th2.join(); cout << countP << " data produced\n"; cout << countC << " data consumed\n"; return 0; } void producer(queue *qP){ int added = 1, i ; for(i=1;i<=500000;++i) { int data, err; if(added) { data = rand() ; added = 0 ; } err = qP->addQ(data) ; if(err == OK) { added = 1 ; cout << "Produced Data " << ++countP << " " << data << "\n" ; } } } void consumer(queue *qP) { int i ; for(i=1; i<= 500000; ++i) { int data, dataOK; dataOK = qP -> frontQ(data); if(dataOK == OK){ qP -> deleteQ() ; cout << "\tConsumed Data " << ++countC << " " << data << "\n" ; } } }