Sha256: 7b9849534ea761bd170b9156cd3b08b1b7e32a40ca60cc197785edb947498693

Contents?: true

Size: 1.29 KB

Versions: 9

Compression:

Stored size: 1.29 KB

Contents

#ifndef LIMONP_BOUNDED_BLOCKING_QUEUE_HPP
#define LIMONP_BOUNDED_BLOCKING_QUEUE_HPP

#include "BoundedQueue.hpp"

namespace limonp {

template<typename T>
class BoundedBlockingQueue : NonCopyable {
 public:
  explicit BoundedBlockingQueue(size_t maxSize)
    : mutex_(),
      notEmpty_(mutex_),
      notFull_(mutex_),
      queue_(maxSize) {
  }

  void Push(const T& x) {
    MutexLockGuard lock(mutex_);
    while (queue_.Full()) {
      notFull_.Wait();
    }
    assert(!queue_.Full());
    queue_.Push(x);
    notEmpty_.Notify();
  }

  T Pop() {
    MutexLockGuard lock(mutex_);
    while (queue_.Empty()) {
      notEmpty_.Wait();
    }
    assert(!queue_.Empty());
    T res = queue_.Pop();
    notFull_.Notify();
    return res;
  }

  bool Empty() const {
    MutexLockGuard lock(mutex_);
    return queue_.Empty();
  }

  bool Full() const {
    MutexLockGuard lock(mutex_);
    return queue_.Full();
  }

  size_t size() const {
    MutexLockGuard lock(mutex_);
    return queue_.size();
  }

  size_t capacity() const {
    return queue_.capacity();
  }

 private:
  mutable MutexLock          mutex_;
  Condition                  notEmpty_;
  Condition                  notFull_;
  BoundedQueue<T>  queue_;
}; // class BoundedBlockingQueue

} // namespace limonp

#endif // LIMONP_BOUNDED_BLOCKING_QUEUE_HPP

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
cppjieba_rb-0.4.2 ext/cppjieba/deps/limonp/BoundedBlockingQueue.hpp
cppjieba_rb-0.4.1 ext/cppjieba/deps/limonp/BoundedBlockingQueue.hpp
jieba-rb-5.0.0 ext/cppjieba/deps/limonp/BoundedBlockingQueue.hpp
cppjieba_rb-0.3.3 ext/cppjieba/deps/limonp/BoundedBlockingQueue.hpp
cppjieba_rb-0.3.1 ext/cppjieba/deps/limonp/BoundedBlockingQueue.hpp
cppjieba_rb-0.3.0 ext/cppjieba/deps/limonp/BoundedBlockingQueue.hpp
cppjieba_rb-0.2.3 ext/cppjieba/deps/limonp/BoundedBlockingQueue.hpp
cppjieba_rb-0.2.2 ext/cppjieba/deps/limonp/BoundedBlockingQueue.hpp
cppjieba_rb-0.2.1 ext/cppjieba/deps/limonp/BoundedBlockingQueue.hpp