生产者消费者-阻塞队列模板类

#ifndef __BLOCKQUEUE_H__
#define __BLOCKQUEUE_H__

#include <condition_variable>
#include <mutex>
#include <queue>
#include <vector>
#include <assert.h>
#include <iostream>
#include <thread>

using namespace std;
using namespace std::chrono_literals;

/********************************************************************************
类名 : BlockQueue
Description: 阻塞队列模板类,用于生产者消费者场景
Example: BlockQueue<T> oQueue()
*******************************************************************************/
template<typename T>
class BlockQueue {
public:
	// make class non-copyable
	BlockQueue(const BlockQueue<T>&) = delete;
	BlockQueue& operator=(const BlockQueue<T>&) = delete;

	/**
     * @brief 构造函数,可设置队列大小
     *
     * @param[in] maxSize 队列大小
     * @return 无
    */
	explicit BlockQueue<T>(size_t maxSize = INT_MAX)
		: m_nMaxSize(maxSize), m_bQuit(false)
	{

	}

	/**
    * @brief 向队列里添加对象,可设置超时时间
    *
    * @param[in] x 添加的对象
	* @param[in] timeout 超时时间
    * @return true 成功; false 失败
    */
    bool push(const T& x, int timeout = INT_MAX) 
    {
        if (m_bQuit) {
            return false;
        }

		std::unique_lock<std::mutex> locker(m_Mutex);
		m_NotFullCV.wait_for(locker, timeout*1s, 
            [this]() {return (m_queue.size() < m_nMaxSize) || m_bQuit;});			

        if (m_bQuit || m_queue.size() == m_nMaxSize) {
            return false;
        }

		m_queue.push(x);
		m_NotEmptyCV.notify_one();
        return true;
	}

	/**
    * @brief 从队列里获取对象,可设置超时时间
    *
    * @param[out] x 获取的对象
	* @param[in] timeout 超时时间
    * @return true 成功; false 失败
    */
	bool pop(T& outRes, int timeout = INT_MAX) 
    {
        if (m_bQuit) {
            return false;
        }

		std::unique_lock<std::mutex> locker(m_Mutex);
		m_NotEmptyCV.wait_for(locker, timeout*1s, 
            [this]() {return !m_queue.empty() || m_bQuit;} );

		if (m_bQuit || m_queue.empty()) {
            return false;
        }
		
		outRes = m_queue.front();
        m_queue.pop();
		m_NotFullCV.notify_one();

		return true;
	}

	// 获取队列是否为空
	bool empty() const 
    {
		std::unique_lock<std::mutex> locker(m_Mutex);
		return m_queue.empty();
	}

	// 获取队列当前容量
	size_t size() const 
    {
		std::unique_lock<std::mutex> locker(m_Mutex);
		return m_queue.size();
	}

	// 返回队列最大容量
	size_t maxSize() const 
    {
		return m_nMaxSize;
	}

	// 退出,以便阻塞的pop和push能够返回
    void quit()
    {
        m_bQuit = true;
        m_NotEmptyCV.notify_all();
        m_NotFullCV.notify_all();
    }

private:
	mutable std::mutex m_Mutex;
	std::condition_variable m_NotEmptyCV;
	std::condition_variable m_NotFullCV;
	size_t m_nMaxSize;
	std::queue<T> m_queue;
    bool m_bQuit;
};

#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_21239475

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值