c++ blockqueue 记录

#pragma once 
#include 
#include 
#include 
#include 
namespace myBlockQueue {

	template 
	class BlockDeque {
	public:
		explicit BlockDeque(size_t MaxCapacity = 1000)
			:capacity_(MaxCapacity)
		{
			assert(MaxCapacity > 0);
			isClose = false;
		}
		~BlockDeque()
		{
			Close();
		}
		void clear()
		{
			std::lock_guard locker(mtx_);
			deq_.clear();
			condProducer_.notify_all(); // 当前为空只需要唤醒生产者
		}
		bool empty()
		{
			std::lock_guard locker(mtx_);
			return deq_.empty();
		}
		bool full()
		{
			std::lock_guard locker(mtx_);
			return deq_.size() >= capacity_;
		}
		void Close()
		{
			{
				std::lock_guard locker(mtx_);
				//deq_.clear(); // 关闭不一定需要清除所有元素,即使关闭了也还是可以取元素
				isClose_ = true;
			}
// 需要唤醒 否则push 或者pop 可以一直阻塞
			condProducer_.notify_all();
			condConsumer_.notify_all();
		}
		size_t size()
		{
			std::lock_guard locker(mtx_);
			return deq_.size();
		}
		size_t capacity()
		{
			std::lock_guard locker(mtx_);
			return capacity_;
		}
		T front()
		{
			std::lock_guard locker(mtx_);
			if (deq_.empty())
				throw std::runtime_error("Deque is empty");
			return deq_front();
		}
		T back()
		{
			std::lock_guard locker(mtx_);
			if (deq_.empty())
				throw std::runtime_error("Deque is empty");
			return deq_.back();
		}
		void push_back(const T& item)
		{
			std::unique_lock locker(mtx_);
			/*while (deq_.size() >= capacity_)
				condProducer_.wait(locker);*/
			condProducer_.wait(locker,
				[this]() {return deq_.size() < capacity_ || isClose_; }
			);
			if (isClose_) throw std::runtime_error("Queue is closed");
			deq_.push_back(item);
			condConsumer_.notify_one();
		}
		// 如果是关闭状态不可以做push 操作
		void push_front(constT& item)
		{
			std::unique_lock locker(mtx_);
			/*while (deq_.size() >= capacity_)
				condProducer_.wait(locker);*/
			condProducer_.wait(locker, [this]() {return deq_.size() < capacity_ || isClose_; });
			if (isClose_) throw std::runtime_error("Queue is closed");
			deq_.push_front(item);
			condConsumer_.notify_one();
		}
		bool pop(T& item)
		{
			std::unique_lock locker(mtx_);
			/*while (deq_.empty())
			{
				condConsumer_.wait(locker);
				if (isClose_)
					return false;
			}*/
			condConsumer_.wait(locker, [this]() {return !deq_.empty()|| isClose_});
			if (isClose_ && deq_.empty())
				return false;

			item = deq_.front();
			deq_.pop_front();
			condProducer_.notify_one();
			return true;
		}
		bool pop(T& item, int timeout)
		{
			std::unique_lock locker(mtx_);
		/*	while (deq_.empty())
			{
				if (condConsumer_.wait_for(locker, std::chrono::seconds(timeout))
					== std::cv_status::timeout)
				{
					return false;
				}
				if (isClose_) {
					return false;
				}
			}*/
			if (!condConsumer_.wait_for(locker, std::chrono::seconds(timeout),
				[this]() {return !deq_.empty() || isClose_; }))
			{
				return false;
			}

			if (isClose_ && deq_.empty()) return false;

			item = deq_.front();
			deq_.pop_front();
			condProducer_.notify_one();
			return true;
		}
		void flush()
		{
			condConsumer_.notify_one();
		}
	private:
	
		std::deque deq_;
		size_t capacity_;
		std::mutex mtx_;
		bool isClose_;
		
		std::condition_variable condConsumer_;
		std::condition_variable condProducer_;
	};
}

你可能感兴趣的:(c++,并发,c++,开发语言)