| 15 | void setMaxCapacity(size_t capacity) { max_capacity_ = capacity; } |
| 16 | |
| 17 | void push(const T& value, size_t capacity, bool finished = false) { |
| 18 | CHECK(!done_) << "must not call push again if *finished* is set true"; |
| 19 | if (capacity > max_capacity_) { |
| 20 | LL << "push obj with size " << capacity |
| 21 | << " into queue with capacity " << max_capacity_ |
| 22 | << ". you will be blocked here forever..."; |
| 23 | } |
| 24 | // do not insert |
| 25 | if (finished == false && capacity == 0) return; |
| 26 | std::unique_lock<std::mutex> l(mu_); |
| 27 | full_cond_.wait(l, [this, capacity]{ |
| 28 | return (capacity + cur_capacity_ <= max_capacity_); }); |
| 29 | queue_.push(std::move(std::make_pair(value, capacity))); |
| 30 | cur_capacity_ += capacity; |
| 31 | done_ = finished; |
| 32 | empty_cond_.notify_all(); |
| 33 | } |
| 34 | |
| 35 | bool pop(T& value) { |
| 36 | std::unique_lock<std::mutex> l(mu_); |