Question

In C++, code a circular linked buffer to be used with a producer/consumer synchronized solution

In C++, code a circular linked buffer to be used with a producer/consumer synchronized solution

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <mutex>
#include <deque>
#include <chrono>
#include <condition_variable>
#include <thread>
#include <iostream>

using std::deque;
std::mutex mux,cout_mux;
std::condition_variable condi;

class MyBuffer
{
public:
void add(int mynum) {
while (true) {
std::unique_lock<std::mutex> locker(mux);
condi.wait(locker, [this](){return buffer_.size() < size_;});
buffer_.push_back(mynum);
locker.unlock();
condi.notify_all();
return;
}
}
int remove() {
while (true)
{
std::unique_lock<std::mutex> locker(mux);
condi.wait(locker, [this](){return buffer_.size() > 0;});
int back = buffer_.back();
buffer_.pop_back();
locker.unlock();
condi.notify_all();
return back;
}
}
MyBuffer() {}
private:
deque<int> buffer_;
const unsigned int size_ = 10;
};


class MyConsumer
{
public:
MyConsumer(MyBuffer* buffer)
{
this->buffer_ = buffer;
}
void run() {
while (true) {
int num = buffer_->remove();
cout_mux.lock();
std::cout << "Consumed : " << num << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
cout_mux.unlock();
}
}
private:
MyBuffer *buffer_;
};

class MyProducer
{
public:
MyProducer(MyBuffer* buffer)
{
this->buffer_ = buffer;
}
void run() {
while (true) {
int num = std::rand() % 100;
buffer_->add(num);
cout_mux.lock();
std::cout << "Produced : " << num << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
cout_mux.unlock();
}
}
private:
MyBuffer *buffer_;
};


int main() {
MyBuffer buff;
MyProducer pro(&buff);
MyConsumer con(&buff);

std::thread producer_thread(&MyProducer::run, &pro);
std::thread consumer_thread(&MyConsumer::run, &con);

producer_thread.join();
consumer_thread.join();
getchar();
return 0;
}

Add a comment
Know the answer?
Add Answer to:
In C++, code a circular linked buffer to be used with a producer/consumer synchronized solution
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are given a sample code that when edited/ improved/completed -will work as a producer consumer...

    You are given a sample code that when edited/ improved/completed -will work as a producer consumer synchronized solution. In addition to the counter, you will implement a circular linked list as a buffer of size 200 that stores data items 0 or 1. The producer and consumer take *random* turns to produce and consume- *random* numbers of elements ( turning 1 to a 0 and visa-versa-each time, as discussed in class). After each turn of the producer and/or consumer, your...

  • Producer and Consumer Code in C++ The program will contain two methods- one each for the...

    Producer and Consumer Code in C++ The program will contain two methods- one each for the producer and consumer. Both methods share access to an integer array buffer of size 50 with all values initialized to 0 at the beginning and an integer variable counter initialized to 0. As given below, the producer accesses the buffer elements and updates the element to 1 ( indicating production). The consumer changes a buffer elements to 0 (indicating consumption). Enhancements and modifications: You...

  • write a C/C++ program which simulates the Producer/Consumer program in Figure 5.16 with a buffer size...

    write a C/C++ program which simulates the Producer/Consumer program in Figure 5.16 with a buffer size of one thousand. Allow the Producer to generate one million items. Use ten consumers. The program needs to perform a normal exit process after all items are consumed. Both the Producer (singular) and Consumers are to be runs as separate processes generated via fork(). The program must us Linux semaphores. The program must clean up the semaphores used and zombies created before termination. Report...

  • 1) Producer/Consumer again Consider the bounded buffer Producer/Consumer problem we discussed in class. Assume the buffer...

    1) Producer/Consumer again Consider the bounded buffer Producer/Consumer problem we discussed in class. Assume the buffer size is now 10. Assume the Producer process enters 3 items at a time into the buffer. It will only do this if there are at least 3 empty spots in the buffer. Assume the Consumer process will remove items two at a time, and will only do so if there are at least two items in the buffer. In addition to the Producer...

  • Code in C++ Modify “Producer and Consumer Problem” from lecture note so that it can use...

    Code in 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 program will then create a separate threads, producer and consumer thread. 3. Producer thread generates a random number through...

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

    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...

  • Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to...

    Solve the Consumer/Producer problem using semaphores. A skeleton program (Save it as producer_consumer.c) is provided to you: The main function creates 2 threads: consumer represents the consumer and executes the consume function, and producer represents the producer and executes the  produce function. You should declare and initialize 3 semaphores. Those semaphores should be used in the consume(..) and produce(...) functions. #include <stdio.h> #include <stdlib.h> #include <pthread.h> //compile and link with -pthread #define BUFFER_SIZE 10 int buffer[BUFFER_SIZE]; int in, out; int num;...

  • Consider the Producer-Consumer problem Assume that there are 2 producers (P1 and P2) and 1 consumer...

    Consider the Producer-Consumer problem Assume that there are 2 producers (P1 and P2) and 1 consumer (C1). The maximum number of items in the queue is 100. Consider the following solution The functions qfull enque and deque are the standard functions discussed in class Assume they have been implemented correctly by one of the methods Semaphore ni=0, mutex=1; /* ni= number of items currently in the queue.*/ /* mutex is used to provide critical section. */ Code of a Producer...

  • I need a help to solve this problem I want to create a code that implement bounded buffer problem by using semaphore. so...

    I need a help to solve this problem I want to create a code that implement bounded buffer problem by using semaphore. so use int buffer[10]; // buffer is global variable and buffersize is 10 and there is only one producer and one consumer. 1) Producer() - At every 200msec, generates random positive integer and add to the buffer. - Then print current state of buffer (numbers and in/out indices) -Also print current state of two semaphore which are full...

  • The producer-consumer problem can be solved in different ways. If we use the buffering technique, which...

    The producer-consumer problem can be solved in different ways. If we use the buffering technique, which of the following sizes would be the most reliable. a) A zero capacity as the chances of the producer and consumer access the buffer at the same time is non-existent. b) A bounded capacity that is sized by the max of the incoming rate. c) An unbounded capacity. Then you never have any synchronization issues. d) A dynamic capacity that automatically grows as needed.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT