/***************************************************************** File: producer_consumer.cc Description: A program that simulates a producer-consumer model with pthreads. Author: Dana Vrajitoru Organization: IUSB Date: September, 2020 Compilation command: g++ producer_consumer.cc main.cc -o prodc -lpthread Run with: prodc or ./prodc ******************************************************************/ #include #include #include using namespace std; #include #include "producer_consumer.h" // global variables int data[MAX_ITEMS]; int counter = 0; pthread_mutex_t data_mutex, output_mutex; // Initializes the mutexes we need. void Init_mutexes() { pthread_mutex_init(&data_mutex, NULL); pthread_mutex_init(&output_mutex, NULL); } // Creates the threads calling the producer function, then those // calling the consumer function, and to each of them it passes the // pointer to the number of items as the argument. void Create_threads(pthread_t *threads, int nr_producers, int nr_consumers, int &nr_items) { void *ptr_nr_items = &nr_items; for (int i=0; i> nr_producers; cout << "Enter the number of items for each producer" << endl; cin >> nr_items; nr_consumers = nr_producers; } // The classical producer function. void *Producer(void *arg) { int nr_items = *(int *)arg, item; for (int i=0; i