阻塞队列实现
#ifndef BLOCKQUEUE_HPP
#define BLOCKQUEUE_HPP
#include <assert.h>
#include <chrono>
#include <condition_variable>
#include <deque>
#include <mutex>
//阻塞队列
template<class T>
class BlockDeque
{
public:
explicit BlockDeque(size_t MaxCapacity = 1000);
~BlockDeque();
void Close();
void clear();
bool empty();
bool full();
size_t size();
size_t capacity();
T front();
T back();
void push_back(const T& item);
void push_front(const T& item);
bool pop(T& item);
bool pop(T& item, int timeout);
void flush();
private:
std::deque<T> _deq;
size_t _capacity;
bool _isClose;
std::mutex _mtx;
std::condition_variable _consumer;
std::condition_variable _producer;
};
template<class T>
BlockDeque<T>::BlockDeque(size_t MaxCapacity)
: _capacity(MaxCapacity)
{
assert(MaxCapacity > 0);
_isClose = false;
}
template<class T>
BlockDeque<T>::~BlockDeque()
{
Close();
}
template<class T>
void
BlockDeque<T>::Close()
{
{
std::lock_guard<std::mutex> locker(_mtx);
_deq.clear();
_isClose = true;
}
_producer.notify_all();
_consumer.notify_all();
}
template<class T>
void
BlockDeque<T>::flush()
{
_consumer.notify_one();
}
template<class T>
void
BlockDeque<T>::clear()
{
std::lock_guard<std::mutex> locker(_mtx);
_deq.clear();
}
template<class T>
bool
BlockDeque<T>::empty()
{
std::lock_guard<std::mutex> locker(_mtx);
return _deq.empty();
}
template<class T>
bool
BlockDeque<T>::full()
{
std::lock_guard<std::mutex> locker(_mtx);
return _deq.size() >= _capacity;
}
template<class T>
size_t
BlockDeque<T>::size()
{
std::lock_guard<std::mutex> locker(_mtx);
return _deq.size();
}
template<class T>
size_t
BlockDeque<T>::capacity()
{
std::lock_guard<std::mutex> locker(_mtx);
return _capacity;
}
template<class T>
T
BlockDeque<T>::front()
{
std::lock_guard<std::mutex> locker(_mtx);
return _deq.front();
}
template<class T>
T
BlockDeque<T>::back()
{
std::lock_guard<std::mutex> locker(_mtx);
return _deq.back();
}
template<class T>
void
BlockDeque<T>::push_back(const T& item)
{
std::unique_lock<std::mutex> locker(_mtx);
while (_deq.size() >= _capacity) //生产满了,等待消费
{
_producer.wait(locker);
}
_deq.push_back(item);
_consumer.notify_one(); //通知一个消费者
}
template<class T>
void
BlockDeque<T>::push_front(const T& item)
{
std::unique_lock<std::mutex> locker(_mtx);
while (_deq.size() >= _capacity) {
_producer.wait(locker);
}
_deq.push_front(item);
_consumer.notify_one();
}
template<class T>
bool
BlockDeque<T>::pop(T& item)
{
std::unique_lock<std::mutex> locker(_mtx);
while (_deq.empty()) {
//线程阻塞,解锁,等待唤醒; 唤醒后不断尝试加锁,直至成功
_consumer.wait(locker);
if (_isClose) {
return false;
}
}
item = _deq.front();
_deq.pop_front();
_producer.notify_one();
return true;
}
template<class T>
bool
BlockDeque<T>::pop(T& item, int timeout)
{
std::unique_lock<std::mutex> locker(_mtx);
while (_deq.empty()) {
if (_consumer.wait_for(locker, std::chrono::seconds(timeout)) ==
std::cv_status::timeout) {
return false;
}
if (_isClose) {
return false;
}
}
item = _deq.front();
_deq.pop_front();
_producer.notify_one();
return true;
}
#endif // BLOCKQUEUE_HPP
/*vim 替换
% 当前文件(省略表示当前行)
s 替换
%s/a/b 当前文件 /a替换成/b
*/