自定义std::queue包装器的线程安全队列、循环队列

shared_queue.h

#ifndef AUDFLY_SHARED_QUEUE_H_
#define AUDFLY_SHARED_QUEUE_H_

#include <queue>
#include <mutex>
#include <condition_variable>

namespace audfly {
//@typename T:元素类型
//@typename Container:真正的底层存储
template <
	typename T,
    typename Container = std::deque<T>
>
class shared_queue {
	public:
    	using size_type = typename Container::size_type;
	public:
		shared_queue() = default;
    	~shared_queue() {}
    	
 		//由于是条件变量,禁止复制/移动拷贝和赋值运算符
		shared_queue(shared_queue &&) = delete;
	    shared_queue(shared_queue const &) = delete;
	    shared_queue& operator=(shared_queue &&) = delete;
	    shared_queue& operator=(shared_queue const &) = delete;
		
		////返回对队列第一个元素数据的只读(常量)引用。
		//该元素将是调用pop()时第一个被移除的元素。
		const T& front() {
	        std::unique_lock<std::mutex> lock(mu_);
	        while (underlying_queue_.empty()) {
	            cond_.wait(lock);
	        }
	        return underlying_queue_.front();
    	};
		//从队列前端移除一个元素,如果队列为空则不执行任何操作。
    	void pop() {
	        if (underlying_queue_.empty()) {
	            return;
	        }	
	        std::unique_lock<std::mutex> lock(mu_);
	        underlying_queue_.pop();
	        lock.unlock();
    	};
    	//将给定的元素值压入队列末尾,通过const引用传递参数
		void push(const T& element) {
	        std::unique_lock<std::mutex> lock(mu_);
	        underlying_queue_.push(element);
	        lock.unlock();	
	        cond_.notify_one();
    	};
		//将给定的元素值压入队列末尾。
		//通过std::move()传递带有右值的shared_queue实例。例如:
		//void f(shared_queue<string>&& queue);
		//f (std::move(queue));
	    void push(T&& movable_element) {
	        std::unique_lock<std::mutex> lock(mu_);
	        underlying_queue_.push(std::move(movable_element));
	        lock.unlock();
	
	        cond_.notify_one();
	    };
	
	    //返回底层容器中的元素个数。
	    size_type size() const noexcept {
	        return underlying_queue_.size();
	    }
	
	    //如果队列为空则返回true。
	    bool empty() const noexcept {
	        return underlying_queue_.empty();
	    }
	
	private:
	    std::queue<T, Container> underlying_queue_;
	    std::mutex mu_;
	    std::condition_variable cond_;
}; 
}//namespace audfly
#endif // AUDFLY_SHARED_QUEUE_H_

//定义一个shared_queue变量:
//audfly::shared_queue<std::string>   string_shared_queue;

circular_queue.h

#ifndef AUDFLY_CIRCULAR_QUEUE_H_
#define AUDFLY_CIRCULAR_QUEUE_H_

#include <stdexcept>

#include <audfly/shared_queue.h>

namespace audfly {
template <
    typename T,
    typename Container = std::deque<T>
>
class circular_queue {

public:
    using size_type = typename Container::size_type;

public:
    circular_queue(std::size_t capacity): capacity_(capacity) {
        if (capacity == 0) {
            throw std::invalid_argument("capacity should be a positive integer");
        }
    };

    ~circular_queue() {};
    
    circular_queue(circular_queue &&) = delete;
    circular_queue(circular_queue const &) = delete;
    circular_queue &operator=(circular_queue &&) = delete;
    circular_queue &operator=(circular_queue const &) = delete;

    const T &front() {
        return underlying_shared_queue_.front();
    };
    void pop() {
        underlying_shared_queue_.pop();
    };

    void push(const T &element) {
        std::lock_guard<std::mutex> lock(mu_);
        if (is_full())
        {
            underlying_shared_queue_.pop();
        }

        underlying_shared_queue_.push(element);
    };

    
    void push(T &&movable_element) {
        std::lock_guard<std::mutex> lock(mu_);

        if (is_full()) {
            underlying_shared_queue_.pop();
        }

        underlying_shared_queue_.push(std::move(movable_element));
    };

    size_type size() const noexcept {
        return underlying_shared_queue_.size();
    }

    bool empty() const noexcept {
        return underlying_shared_queue_.empty();
    }

    bool is_full() const noexcept {
        return underlying_shared_queue_.size() == capacity_;
    }

private:
    audfly::shared_queue<T, Container> underlying_shared_queue_;
    std::size_t capacity_;
    std::mutex mu_;
};

} // namespace audfly

#endif // AUDFLY_CIRCULAR_QUEUE_H_

//定义一个circular_queue变量:
//audfly::circular_queue<std::string>   string_circular_queue;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值