Question: I've got to implement the code in the attached image using only C++'s threads, mutexes, and condition_variable libraries. I am pretty lost as to where

I've got to implement the code in the attached image using only C++'s threads, mutexes, and condition_variable libraries. I am pretty lost as to where to start as I don't fully understand C++ threads, mutexes, or condition variables and would appreciate some help on this. It does have to be able to do a few of the following things as well, which I will list below:

Use a char array of size N as the shared buffer.

Producer should randomly generate ( >N ) uppercase letters and insert them into the next available space in the buffer, then print out the letter in lowercase form to standard output.

The consumer should consume the uppercase characters from the buffer and print them out unchanged to standard output.

The main function should wait a short amount of time to give the producer time to partially fill the buffer before beginning the consumer thread.

The producer and consumer threads must only access one element at a time. Do not add or remove multiple items within the critical section.

I've got to implement the code in the attached image using only

#include #include #define MAX 1000000000 pthread mutex t the_mutex; pthread cond t condc, condp int buffer 0; how many numbers to produce / used for signaling produce data */ /* buffer used between producer and consumer void "producer(void "ptr) int i for (i-1; i MAX: ++) pthread-mutex-lock(&the-mutex); /* get exclusive access to buffer */ while (buffer! 0) pthread_cond_wait(&condp, &the mutex) buffer i pthread_cond_signal(&condc); pthread_mutex_unlock(&the_mutex); release access to buffer /* put item in buffer "f /* wake up consumer */ pthread_exit(0) void *consumer(void *ptr) consume data */ int i for (i 1; i MAX; i++) pthread_mutex_lock(&the_mutex); get exclusive access to buffer while (buffer ==0 ) pthread_cond_wait(&condc, &the-mutex) buffer 0; pthread_cond_signal(&condp) pthread_mutex_unlock(&the_mutex); release access to buffer take item out of buffer /* wake up producer pthread exit(o) int main(int argc, char "argv) pthread_t pro, con; pthread_mutex_init(&the_mutex, 0) pthread_cond_init(&condc, 0); pthread_cond_init(&condp, 0); pthread_create(&con, 0, consumer, 0); pthread_create(&pro, 0, producer, 0); pthread join(pro, 0) pthread_join(con, 0) pthread_cond_destroy(&condc) pthread_cond_destroy(&condp) pthread_mutex destroy/&the_mutex)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!