// excerpts from http://code.google.com/p/muduo/ // // Use of this source code is governed by a BSD-style license // that can be found in the License file. // // Author: Shuo Chen (giantchen at gmail dot com) #ifndef MUDUO_BASE_BLOCKINGQUEUE_H #define MUDUO_BASE_BLOCKINGQUEUE_H #include "Condition.h" #include "Mutex.h" #include #include #include namespace muduo { template class BlockingQueue : boost::noncopyable { public: BlockingQueue() : mutex_(), notEmpty_(mutex_), queue_() { } void put(const T& x) { MutexLockGuard lock(mutex_); queue_.push_back(x); notEmpty_.notify(); } T take() { MutexLockGuard lock(mutex_); // always use a while-loop, due to spurious wakeup while (queue_.empty()) { notEmpty_.wait(); } assert(!queue_.empty()); T front(queue_.front()); queue_.pop_front(); return front; } size_t size() const { MutexLockGuard lock(mutex_); return queue_.size(); } private: mutable MutexLock mutex_; Condition notEmpty_; std::deque queue_; }; } #endif // MUDUO_BASE_BLOCKINGQUEUE_H