0% found this document useful (0 votes)
5 views3 pages

C++ Producer-Consumer Problem Implementation

Uploaded by

ajayjain3901
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

C++ Producer-Consumer Problem Implementation

Uploaded by

ajayjain3901
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Powered by AI

Copilot
Sure! Let's break down the Producer-Consumer problem and implement it in
C++.

Aim
To implement the Producer-Consumer problem using C++ with
synchronization mechanisms to ensure proper coordination between the
producer and consumer processes.

Algorithm with Pseudo Code

Producer
1. Loop indefinitely:
○ Produce an item.
○ Wait if the buffer is full.
○ Add the item to the buffer.
○ Signal that the buffer is not empty.

Consumer
2. Loop indefinitely:
○ Wait if the buffer is empty.
○ Remove an item from the buffer.
○ Signal that the buffer is not full.
○ Consume the item.

Program
Here's a C++ implementation using threads, mutexes, and condition variables:
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>

std::mutex mtx;
std::condition_variable cv;
std::queue<int> buffer;
const unsigned int maxBufferSize = 10;

void producer(int id) {


int item = 0;
while (true) {
std::unique_lock<std::mutex> lock(mtx);
[Link](lock, [] { return [Link]() < maxBufferSize; });
[Link](item);
Powered by AI
std::cout << "Producer " << id << " produced item " << item+
+ << std::endl;
[Link]();
cv.notify_all();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}

void consumer(int id) {


while (true) {
std::unique_lock<std::mutex> lock(mtx);
[Link](lock, [] { return ![Link](); });
int item = [Link]();
[Link]();
std::cout << "Consumer " << id << " consumed item " << item
<< std::endl;
[Link]();
cv.notify_all();
std::this_thread::sleep_for(std::chrono::milliseconds(150));
}
}

int main() {
std::thread producers[2], consumers[2];
for (int i = 0; i < 2; ++i) {
producers[i] = std::thread(producer, i + 1);
consumers[i] = std::thread(consumer, i + 1);
}
for (int i = 0; i < 2; ++i) {
producers[i].join();
consumers[i].join();
}
return 0;
}

Sample Input and Output


Since this is a multithreaded program, there isn't a traditional input/output.
However, the console output will look something like this:
Producer 1 produced item 0
Producer 2 produced item 0
Consumer 1 consumed item 0
Producer 1 produced item 1
Consumer 2 consumed item 0
Producer 2 produced item 1
Consumer 1 consumed item 1
...
This implementation ensures that the producer and consumer threads are
properly synchronized, avoiding race conditions and ensuring that the buffer
does not overflow or underflow.
Powered by AI
Feel free to ask if you have any questions or need further assistance!

Source(s)
1. Producer Consumer Problem and its Implementation with C++
2. Producer-Consumer Problem and its Implementation with C++
3. Producer Consumer Problem: Multithreading - Austin G. Walters
4. Producer/Consumer Queues in C++ | Eu, Mircea
5. Producer Consumer problem Free Cpp

You might also like