Skip to content

Commit

Permalink
simplify blocking queue
Browse files Browse the repository at this point in the history
  • Loading branch information
longjon committed Mar 20, 2015
1 parent 7c3282e commit ede1f2a
Showing 1 changed file with 11 additions and 31 deletions.
42 changes: 11 additions & 31 deletions include/caffe/util/blocking_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace caffe {
template<typename T>
class blocking_queue {
public:
explicit blocking_queue() { }
virtual ~blocking_queue() { }

void push(const T& t) {
boost::mutex::scoped_lock lock(mutex_);
queue_.push(t);
Expand All @@ -21,51 +24,28 @@ class blocking_queue {
return queue_.empty();
}

bool try_pop(T& t) {
boost::mutex::scoped_lock lock(mutex_);

if (queue_.empty())
return false;

t = queue_.front();
queue_.pop();
return true;
}

T pop(const string& log_on_wait = "") {
T pop() {
T t = peek();
boost::mutex::scoped_lock lock(mutex_);

while (queue_.empty()) {
if (!log_on_wait.empty()) {
time_t now = time(0);
if (now - last_wait_log_ > 5) {
last_wait_log_ = now;
LOG(INFO) << log_on_wait;
}
}
condition_.wait(lock);
}

T t = queue_.front();
queue_.pop();
return t;
}

// Return element without removing it
T peek() {
boost::mutex::scoped_lock lock(mutex_);

while (queue_.empty())
condition_.wait(lock);

while (queue_.empty()) {
cond_push_.wait(lock);
}
return queue_.front();
}

private:
std::queue<T> queue_;
mutable boost::mutex mutex_;
boost::condition_variable condition_;
time_t last_wait_log_;
boost::condition_variable cond_push_;

DISABLE_COPY_AND_ASSIGN(blocking_queue);
};

} // namespace caffe
Expand Down

0 comments on commit ede1f2a

Please sign in to comment.