// file : blockBoundQueue.h
#ifndef YANG_BLOCKBOUNDQUEUE
#define YANG_BLOCKBOUNDQUEUE
#include <mutex>
#include <condition_variable>
#include <queue>
#include <cstdio>
namespace yang{
#define _FULL 4 //
template <typename _Tp>
class BlockBoundQueue
{
public:
BlockBoundQueue(size_t bound = _FULL) :bound_(bound){}
BlockBoundQueue(const BlockBoundQueue&) = delete;
BlockBoundQueue& operator=(const BlockBoundQueue&) = delete;
void push(const _Tp& value)
{
std::unique_lock<std::mutex> lck(mutex_);// 利用RAII技法,将mutex_托管给lck
while (count_ + 1 == bound_)// 用while防止虚假唤醒
{
printf("the queue is full , waiting for the consumer consuming !\n");
notFull_.wait(lck); //等待队列非满条件发生
}
count_++;
queue_.push(value);
notEmpty_.notify_one();//通知队列非空,不能用all,读者自行思考为什么
}
_Tp get()
{
std::unique_lock<std::mutex> lck(mutex_);
while (queue_.empty())
{
printf("the queue is empty , waiting for the producer producing !\n");
notEmpty_.wait(lck);//等待队列为非空
}
_Tp front(queue_.front());
queue_.pop();
count_--;
notFull_.notify_one();//通知队列为非满,请注意不能用all
return front;
}
private:
std::mutex mutex_;
std::condition_variable notEmpty_;
std::condition_variable notFull_;
std::queue<_Tp> queue_;
size_t count_{ 0 };
size_t bound_;
};
}
#endif
//在visual studio 2013上编译运行通过
// file : main.cpp
#include <cstdio>
#include <cstdlib>
#include <thread>
#include "BlockBoundQueue.h"
yang::BlockBoundQueue<int> blockboundqueue_;// 全局的缓冲边界队列
const int total = 16; // 商品总数
void consumer(size_t id,size_t n); // 消费者
void producer(size_t id,size_t n); // 生产者
int main()
{
std::thread consumer1(consumer,0, 5);
std::thread consumer2(consumer,1, 5);
std::thread consumer3(consumer,2, 6);
std::thread producer1(producer,0, 8);
std::thread producer2(producer,1, 8);
consumer1.join();
consumer2.join();
consumer3.join();
producer1.join();
producer2.join();
system("pause");
return 0;
}
void consumer(size_t id,size_t n)
{
for (auto i = 0; i < n; ++i)
{
std::this_thread::sleep_for(std::chrono::seconds(2));
int item = blockboundqueue_.get();
printf("the %d^th consumer thread has consumed the %d^th item\n", id,item);
}
}
void producer(size_t id,size_t n)
{
for (int i = 0; i < n; ++i)
{
blockboundqueue_.push(i);
printf("the %d^th producer thread has produced the %d^th item\n",id, i);
}
}
【转】https://blog.csdn.net/dhz625/article/details/72026666