Question
In this laboratory, you will be implementing the Producer-Consumer Pattern in a bounded-buffer. Our bounded-buffer of choice is a stack (std::stack - cppreference.com B_). Make
In this laboratory, you will be implementing the Producer-Consumer Pattern in a bounded-buffer. Our bounded-buffer of choice is a stack (std::stack - cppreference.com B_). Make the buffer size (stack's maximum capacity) to not to exceed having 10000 elements inside. This is your shared data.
Create a method called producer) this method will generate a random number from 1 to 10 and whatever number it generated it will be pushed into the stack buffer. Sleep for a random time between (1 to 100ms) before adding another number again to the buffer do this for 10000 times.
Create a method called consumer() this method will remove an item in the stack buffer and accumulate the removed(popped) value on a certain variable called SUM. Sleep for a random time between (1 to 100ms) before removing another number again from the buffer
Create your main method to produce four threads (2) Producers and (2) Consumer and take note of the following additional tasks.
Since there are 2 producer() threads that produces 10000 random number each for total of 20000 random numbers, there would come to a point (may or may not) that it would reach the maximum limit of our stack buffer which is 10000. Therefore a producer thread has to wait() before generating again a random number and placing it into the stack. Print "Stack Full Producer Waiting [thread-id]" when producer is waiting
The 2 consumer threads will continuously remove an item from the stack, there (may or may not) come to a point where the stack buffer is empty thus the consumer thread has to wait until such time that the stack buffer has content. Print "Stack Empty Consumer Waiting [thread-id]"
The program terminates when both producer finishes its job of adding random numbers into the stack and consumer fully consumes all of the numbers from the stack. You have to think of your own mechanism for detecting this one.
Display the total sum of all of the numbers before the program ends. (To determine if the sum is correct, create a temporary variable that accumulates too everytime the producer generates a number both of them should match at the end of the program, much like our previous laboratory). Display both values to check if the sum is correct.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started