// 生产者-消费者模式 无锁队列
get()时,如果deque里面没有元素了,则会一直阻塞,还有待改进的空间
template <class T>
class BlockingQueue {
public:explicit BlockingQueue() : shutdown_(false) {}~BlockingQueue() {}void put(const T& value) {boost::mutex::scoped_lock lock(mutex_);dq_.push_back(value);cv_.notify_one();}bool get(T& value) {boost::mutex::scoped_lock lock(mutex_);if (!shutdown_ && dq_.empty()) {cv_.wait(lock);}if (shutdown_) {return false;}value = dq_.front();dq_.pop_front();return true;}void Shutdown() {boost::mutex::scoped_lock lock(mutex_);shutdown_ = true;cv_.notify_all();}private:boost::mutex::scoped_lock mutex_;bool shutdown_;boost::condition_variable cv_;std::deque<T> dq_;
};