/******************************************************************** File: producer_consumer.h Description: A program that simulates a producer-consumer model with pthreads. Author: Dana Vrajitoru Organization: IUSB Date: September 2020 *********************************************************************/ #ifndef PRODUCER_CONSUMER_H #define PRODUCER_CONSUMER_H #include const int MAX_ITEMS = 10; // Initializes the mutexes we need; void Init_mutexes(); // 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); // Joins all of the threads so that the program doesn;t end before // they have finished the work. void Synchronise(pthread_t *threads, int no_threads); // Inputs the number of producers and the nummber of items to be // produced by each of them. Sets the number of consumers to be equal // to the number of producers. void Input_data(int &nr_producers, int &nr_consumers, int &nr_items); // The classical producer function. void *Producer(void *); // The classical consumer function. void *Consumer(void *); // Attempts to deposit an item in the array of data. It returns 0 in // case of failure, otherwise 1. bool Deposit(int item); // Attempts to fetch an item from the array of data. It returns 0 in // case of failure, otherwise 1. bool Fetch(int &item); // Tells us if there array of data is full. bool Data_is_full(); // Tells us if there array of data is empty. bool Data_is_empty(); // Produces a random integer between 1 and 10. int Produce_item(); // Writes a number of * characters equal to the value of the item. void Consume(int item); #endif