C++ single producer multiple consumer
This is simple program to explain single producer and multiple consumer, please check the below program, it very important to understand the complete logic so please refer below program.
Source Code with Index:
#include<iostream> #include<thread> #include<mutex> #include<condition_variable> #include<chrono> std::mutex gMtx; std::condition_variable gCv; bool gProdReady = true; bool gConsumReady = false; int gCousumIndex = 1; void producer() { while(true) { std::unique_lock<std::mutex>ul(gMtx); gCv.wait(ul, []{return gProdReady;}); std::cout<<"Producer"<<std::endl; gProdReady = false; gConsumReady = true; std::this_thread::sleep_for(std::chrono::seconds(1)); ul.unlock(); gCv.notify_all(); } } void firstConsumer() { while(true) { std::unique_lock<std::mutex>ul(gMtx); gCv.wait(ul, []{return (gConsumReady && (gCousumIndex%3 == 1));}); std::cout<<"firstConsumer"<<std::endl; gConsumReady = false; gProdReady = true; ++gCousumIndex; std::this_thread::sleep_for(std::chrono::seconds(1)); ul.unlock(); gCv.notify_all(); } } void secondConsumer() { while(true) { std::unique_lock<std::mutex>ul(gMtx); gCv.wait(ul, []{return (gConsumReady && (gCousumIndex%3 == 2));}); std::cout<<"secondConsumer"<<std::endl; gConsumReady = false; gProdReady = true; ++gCousumIndex; std::this_thread::sleep_for(std::chrono::seconds(1)); ul.unlock(); gCv.notify_all(); } } void thirdConsumer() { while(true) { std::unique_lock<std::mutex>ul(gMtx); gCv.wait(ul, []{return (gConsumReady && (gCousumIndex%3 == 0));}); std::cout<<"thirdConsumer"<<std::endl; gConsumReady = false; gProdReady = true; ++gCousumIndex; std::this_thread::sleep_for(std::chrono::seconds(1)); ul.unlock(); gCv.notify_all(); } } int main() { std::cout<<"In Main"<<std::endl; std::thread T1(producer); std::thread T2(firstConsumer); std::thread T3(secondConsumer); std::thread T4(thirdConsumer); T1.join(); T2.join(); T3.join(); T4.join(); return 0; }
Give: g++ -pthread sinlgeProMultiConsum.cpp, Output:
In Main Producer firstConsumer Producer secondConsumer Producer thirdConsumer Producer firstConsumer Producer secondConsumer Producer thirdConsumer Producer firstConsumer Producer secondConsumer Producer thirdConsumer Producer firstConsumer
Source Code with Sleep:
/* Program: Single Produce and multiple Consumer Author: Alpha Master Date: 18 Dec 2021 */ //Header File #include<iostream> #include<thread> #include<mutex> #include<condition_variable> #include<queue> std::mutex gMtx; std::condition_variable gCon; bool gReady = true; bool gDone = false; bool gSec = false; std::queue<int> gQ; void cons() { int j =0; while(j<5) { std::unique_lock<std::mutex>ul(gMtx); gCon.wait(ul, []{return gDone;}); std::cout<<"In Consumer"; std::cout<<"Thread ID:"<<std::this_thread::get_id()<<std::endl; std::cout<<"GetData:"<<gQ.back()<<std::endl; gQ.pop(); gDone = false; gReady = true; ul.unlock(); gCon.notify_one(); std::cout<<"Consumer Done"<<std::endl; std::this_thread::sleep_for(std::chrono::seconds(1)); j++; } return; } void prod() { int i = 0; while(i<10) { std::unique_lock<std::mutex>ul(gMtx); gCon.wait(ul, []{return gReady;}); std::cout<<"In Producer"<<std::endl; i++; gQ.push(i); gReady = false; gDone = true; std::cout<<"Producer Done"<<std::endl; ul.unlock(); gCon.notify_one(); } return; } int main() { std::cout<<"Produce and Consumer"<<std::endl; std::thread P1(prod); std::thread T1(cons); std::thread T2(cons); P1.join(); T1.join(); T2.join(); return 0; }
Output:
Produce and Consumer In Producer Producer Done In ConsumerThread ID:140512470738688 GetData:1 Consumer Done In Producer Producer Done In ConsumerThread ID:140512479131392 GetData:2 Consumer Done In Producer Producer Done In ConsumerThread ID:140512470738688 GetData:3 Consumer Done In Producer Producer Done In ConsumerThread ID:140512479131392 GetData:4 Consumer Done In Producer Producer Done In ConsumerThread ID:140512470738688 GetData:5 Consumer Done In Producer Producer Done In ConsumerThread ID:140512479131392 GetData:6 Consumer Done In Producer Producer Done In ConsumerThread ID:140512470738688 GetData:7 Consumer Done In Producer Producer Done In ConsumerThread ID:140512479131392 GetData:8 Consumer Done In Producer Producer Done In ConsumerThread ID:140512470738688 GetData:9 Consumer Done In Producer Producer Done In ConsumerThread ID:140512479131392 GetData:10 Consumer Done