Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to creatre a C++ program that can modify the producer and consumer. My ma

ID: 3589843 • Letter: I

Question

I need to creatre a C++ program that can modify the producer and consumer. My main programmig language is C++.

Modify "Producer and Consumer Problem" from lecture note so that it can use all buffer space, not "buffersize – 1" as in the lecture note. This program should work as follows:

1. The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffersize and counter limit.

2. The main program will then create separate threads, producer and consumer thread.

3. Producer thread generates a random number through random number generator function and inserts this into buffer and prints the number. Increment counter.

4. Consumer thread goes to the buffer and takes a number in the proper order and prints it out. Increment counter.

5. After counter reaches its limit, both threads should be terminated and return to main.

6. Main program terminates.

Explanation / Answer

#include <string>

#include <iostream>

#include <thread>

using namespace std;

int size;

int counter1=1;

int counter2=1;

int rand();

int* buffer = new int[size];

void first(int limit)

{

while( counter1 != limit )

{

buffer[counter1]=rand();

cout<<" inserted Number "<<buffer[counter1]<<endl;

counter1=counter1+1;

}

}

void second(int limit)

{

while( counter2 != limit )

{

if(buffer[counter2] != 0)

{

cout<<" Printing Number "<<buffer[counter2]<<endl;

counter2=counter2+1;

}

}

}

int main() {

size=0;

int limit=0;

cout<<"Enter buffersize";

cin>>size;

cout<<"Enter counter limit";

cin>>limit;

  

std::thread producer(first,limit);

std::thread consumer(second,limit);

producer.join();

consumer.join();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote