#ifndef BLOCKING_QUEUE_H #define BLOCKING_QUEUE_H //#include //#include #if defined(WIN32) || defined(_WIN64) #include #endif #include #include #include #include #include using namespace std; template class BlockingQueue { private: BlockingQueue(const BlockingQueue& rhs); BlockingQueue& operator =(const BlockingQueue& rhs); mutable std::mutex _mutex; std::condition_variable _condvar; deque _queue; bool isShutDown; T tRet; public: BlockingQueue() : _mutex() , _condvar() , _queue() , isShutDown(false) { } ~BlockingQueue() { ShutDown(); std::cout << "blocking queue release" << std::endl; } void Clear() { lock_guard lock(_mutex); _condvar.notify_all(); _queue.clear(); } void ShutDown() { isShutDown = true; _condvar.notify_all(); _queue.clear(); } bool IsShutDown() { return isShutDown; } void Put(const T task) { lock_guard lock(_mutex); if (!isShutDown) { { _queue.push_back(task); } _condvar.notify_all(); } } T Take() { unique_lock lock(_mutex); if (_queue.size() <= 0) _condvar.wait(lock); if (isShutDown || _queue.empty()) { return tRet; } T front(_queue.front()); _queue.pop_front(); return front; } T Front() { unique_lock lock(_mutex); if (_queue.size() <= 0) _condvar.wait(lock); if (isShutDown || _queue.empty()) { return tRet; } T front(_queue.front()); return front; } size_t Size() const { lock_guard lock(_mutex); return _queue.size(); } }; #endif