- public:
- fifo(int m=0);
- bool enq(T, bool blocking=true);
- void deq(T *);
- bool size();
-
- private:
- std::list<T> q_;
- mutex m_;
- std::condition_variable non_empty_c_; // q went non-empty
- std::condition_variable has_space_c_; // q is not longer overfull
- unsigned int max_; //maximum capacity of the queue, block enq threads if exceeds this limit
-};
-
-template<class T>
-fifo<T>::fifo(int limit) : max_(limit)
-{
-}
-
-template<class T> bool
-fifo<T>::size()
-{
- lock ml(m_);
- return q_.size();
-}
+ public:
+ fifo(size_t limit=0) : max_(limit) {}
+
+ bool enq(T e, bool blocking=true) {
+ lock ml(m_);
+ while (max_ && q_.size() >= max_) {
+ if (!blocking)
+ return false;
+ has_space_c_.wait(ml);
+ }
+ q_.push_back(e);
+ non_empty_c_.notify_one();
+ return true;
+ }