Psuedocode help needed creating producer and consumer threads. Assignment listed
ID: 3759648 • Letter: P
Question
Psuedocode help needed creating producer and consumer threads. Assignment listed below. Use as many of my question tokens as needed.
Introduction
In this assignment you will be simulating a producer-consumer paradigm, with multiple producer and consumer threads. Assignment Write a program that that creates a user configurable number of producer and consumer threads. The first command line argument specifies the number of producer threads, the second specifies the number of consumer threads, and the third specifies the size of the shared queue in terms of how many entries it holds.
Producer Specification: Each producer generates a normally distributed number in the range of 1 to 100, with a mean of 50 and standard deviation of 10. This occurs at a random rate of once every 1000 milliseconds. When generated, this number is added to a synchronized queue that is shared between all producers and consumers. If more than one producer is specified, start each one separated by 200 milliseconds.
Consumer Specification: Each consumer can consume an item from the shared queue once every 2000 milliseconds. If more than one consumer is specified, start each one separated by 400 milliseconds. Every time a consumer consumers an item from the shared queue, report to the console the id of the consumer thread along with the value consumed from the queue. Please assign a human readable id for each thread that is an integer starting at 1 and increasing by 1 for each additional thread.
Other Specifications: Your program should synchronize all accesses to resources to avoid unexpected results. You should decide how to handle cases where the shared queue is empty or full. Create a class with enqueue and dequeue methods (as well as any other necessary methods) to handle the accesses to shared resources (this class should also create/connect to the shared resources). Your program should run until ctrl+c is hit (don’t overcomplicate this!).
Notes Write a makefile that builds your program, including a 'clean' target. You must use the C++11 threading, synchronization, and random number generation as appropriate. Take care to handle invalid program arguments.
Explanation / Answer
Producer and Consumer Problem in C++11 Mutilthreaded Environmnet I have no idea about make file concept. I written in normal C++11 environment and it' working fine and running is fine. Let me know still if you need any modifications..
#include <iostream>
#include <thread>
#include <deque>
#include <mutex>
#include <chrono>
#include <condition_variable>
using std::deque;
std::mutex mu,cout_mu;
std::condition_variable cond;
class Buffer
{
public:
void add(int num) {
while (true) {
std::unique_lock<std::mutex> locker(mu);
cond.wait(locker, [this](){return buffer_.size() < size_;});
buffer_.push_back(num);
locker.unlock();
cond.notify_all();
return;
}
}
int remove() {
while (true)
{
std::unique_lock<std::mutex> locker(mu);
cond.wait(locker, [this](){return buffer_.size() > 0;});
int back = buffer_.back();
buffer_.pop_back();
locker.unlock();
cond.notify_all();
return back;
}
}
Buffer() {}
private:
deque<int> buffer_;
const unsigned int size_ = 10;
};
class Producer
{
public:
Producer(Buffer* buffer)
{
this->buffer_ = buffer;
}
void run() {
while (true) {
int num = std::rand() % 100;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
buffer_->add(num);
cout_mu.lock();
std::cout << "Produced: " << num << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(200));
cout_mu.unlock();
}
}
private:
Buffer *buffer_;
};
class Consumer
{
public:
Consumer(Buffer* buffer)
{
this->buffer_ = buffer;
}
void run() {
while (true) {
int num = buffer_->remove();
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
cout_mu.lock();
std::cout << "Consumed: " << num << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(400));
cout_mu.unlock();
}
}
private:
Buffer *buffer_;
};
int main() {
Buffer b;
Producer p(&b);
Consumer c(&b);
std::thread producer_thread(&Producer::run, &p);
std::thread consumer_thread(&Consumer::run, &c);
producer_thread.join();
consumer_thread.join();
getchar();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.