3、多生產者單消費者模式:
- 與單生產者和單消費者模型不同的是,多生產者-單消費者模型中可以允許多個生產者同時向產品庫中放入產品。所以除了保護產品庫在多個讀寫線程下互斥之外,還需要維護生產者放入產品的計數器
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <deque>
using namespace std;
static const int kItemsToProduce = 20;
std::mutex stdoutMutex;
struct ItemRepository
{
deque<int> itemQueue;
int MaxSize = 10;
int itemCounter=0;
std::mutex mtx;
std::mutex itemCounterMtx;
std::condition_variable repository_notFull;
std::condition_variable repository_notEmpty;
}gItemRepository;
typedef struct ItemRepository ItemRepository;
void ProduceItem(ItemRepository &itemRepo, int item)
{
std::unique_lock<std::mutex> lock(itemRepo.mtx);
itemRepo.repository_notFull.wait(lock, [&itemRepo] {
bool full = itemRepo.itemQueue.size() >= itemRepo.MaxSize;
if (full)
{
std::lock_guard<std::mutex> lock(stdoutMutex);
cout << "倉庫滿了,生產者等待中..." << "thread id = " << std::this_thread::get_id() << endl;
}
return !full;
});
itemRepo.itemQueue.push_back(item);
itemRepo.repository_notEmpty.notify_all();
lock.unlock();
}
int ConsumeItem(ItemRepository &itemRepo)
{
int data;
std::unique_lock<std::mutex> lock(itemRepo.mtx);
itemRepo.repository_notEmpty.wait(lock, [&itemRepo] {
bool empty = itemRepo.itemQueue.empty();
if (empty)
{
std::lock_guard<std::mutex> lock(stdoutMutex);
cout << "倉庫空了,消費者等待中..." << "thread id = " << std::this_thread::get_id() << endl;
}
return !empty;
});
data = itemRepo.itemQueue.front();
itemRepo.itemQueue.pop_front();
itemRepo.repository_notFull.notify_all();
lock.unlock();
return data;
}
void ProducerTask(int th_ID)
{
bool readyToExit = false;
while (true)
{
this_thread::sleep_for(std::chrono::seconds(1));
std::unique_lock<std::mutex> lock(gItemRepository.itemCounterMtx);
if (gItemRepository.itemCounter < kItemsToProduce)
{
gItemRepository.itemCounter++;
ProduceItem(gItemRepository, gItemRepository.itemCounter);
{
std::lock_guard<std::mutex> lock(stdoutMutex);
cout << "Produce Thread " <<th_ID<<"the "<< gItemRepository.itemCounter << " ^th item..." << endl;
}
}
else
{
readyToExit = true;
}
lock.unlock();
if (readyToExit)
break;
}
{
std::lock_guard<std::mutex> lock(stdoutMutex);
cout << "Producer Thread "<<th_ID<<"exit.... " << endl;
}
}
void ConsumerTask()
{
static int item_consumed = 0;
while (true)
{
item_consumed++;
if (item_consumed <= kItemsToProduce)
{
int item = ConsumeItem(gItemRepository);
{
std::lock_guard<std::mutex> lock(stdoutMutex);
cout << "Consume the " << item << "^th item..." << endl;
}
}
else
{
break;
}
}
{
std::lock_guard<std::mutex> lock(stdoutMutex);
cout << "Consumer Thread "<<" exit...." << endl;
}
}
int main()
{
std::thread producer1(ProducerTask,1);
std::thread producer2(ProducerTask,2);
std::thread producer3(ProducerTask,3);
std::thread producer4(ProducerTask,4);
std::thread consumer(ConsumerTask);
producer1.join();
producer2.join();
producer3.join();
producer4.join();
consumer.join();
system("pause");
return 0;
}
倉庫空了,消費者等待中...thread id = 140157878253312
Produce Thread 3the 1 ^th item...
Produce Thread 2the 2 ^th item...
Produce Thread 4the 3 ^th item...
Consume the 1^th item...
Consume the 2^th item...
Consume the 3^th item...
倉庫空了,消費者等待中...thread id = 140157878253312
Produce Thread 1the 4 ^th item...
Consume the 4^th item...
倉庫空了,消費者等待中...thread id = 140157878253312
Produce Thread 2the 5 ^th item...
Produce Thread 3the 6 ^th item...
Produce Thread 4the 7 ^th item...
Consume the 5^th item...
Consume the 6^th item...
Consume the 7^th item...
倉庫空了,消費者等待中...thread id = 140157878253312
Produce Thread 1the 8 ^th item...
Consume the 8^th item...
倉庫空了,消費者等待中...thread id = 140157878253312
Produce Thread 3the 9 ^th item...
Produce Thread 2the 10 ^th item...
Produce Thread 4the 11 ^th item...
Consume the 9^th item...
Consume the 10^th item...
Consume the 11^th item...
倉庫空了,消費者等待中...thread id = 140157878253312
Produce Thread 1the 12 ^th item...
Consume the 12^th item...
倉庫空了,消費者等待中...thread id = 140157878253312
Produce Thread 2the 13 ^th item...
Produce Thread 3the 14 ^th item...
Produce Thread 4the 15 ^th item...
Consume the 13^th item...
Consume the 14^th item...
Consume the 15^th item...
倉庫空了,消費者等待中...thread id = 140157878253312
Produce Thread 1the 16 ^th item...
Consume the 16^th item...
倉庫空了,消費者等待中...thread id = 140157878253312
Produce Thread 3the 17 ^th item...
Produce Thread 2the 18 ^th item...
Produce Thread 4the 19 ^th item...
Consume the 17^th item...
Consume the 18^th item...
Consume the 19^th item...
倉庫空了,消費者等待中...thread id = 140157878253312
Produce Thread 1the 20 ^th item...
Consume the 20^th item...
Consumer Thread exit....
Producer Thread 2exit....
Producer Thread 3exit....
Producer Thread 4exit....
Producer Thread 1exit....