40 std::lock_guard<std::mutex>
lock{
other._lock };
41 this->batches_ =
other.batches_;
42 this->front_index_ =
other.front_index_;
43 this->size_ =
other.size_;
44 this->_notify_size =
other._notify_size;
45 this->_is_empty.store(
other._is_empty.load(std::memory_order_acquire), std::memory_order_release);
46 this->_is_waitable.store(
other._is_waitable.load(std::memory_order_acquire), std::memory_order_release);
51 rows.reserve(
source.size());
52 for (
const auto& row :
source) {
56 this->batches_.push_back(std::move(rows));
57 this->size_ =
source.size();
59 this->_is_empty.store(
source.empty(), std::memory_order_release);
62 bool empty()
const noexcept {
63 return this->_is_empty.load(std::memory_order_acquire);
66 void push_back(
T&&
item) {
67 std::lock_guard<std::mutex>
lock{ this->_lock };
70 this->batches_.push_back(std::move(
batch));
72 this->_is_empty.store(
false, std::memory_order_release);
74 if (this->size_ >= _notify_size) {
75 this->_cond.notify_all();
79 void append_rows(std::vector<T>&& rows) {
84 std::lock_guard<std::mutex>
lock{ this->_lock };
85 this->size_ += rows.size();
86 this->batches_.push_back(std::move(rows));
87 this->_is_empty.store(
false, std::memory_order_release);
89 if (this->size_ >= _notify_size) {
90 this->_cond.notify_all();
94 T pop_front()
noexcept {
95 std::lock_guard<std::mutex>
lock{ this->_lock };
96 T item = std::move(this->batches_.front()[
this->front_index_]);
99 this->discard_exhausted_front_batch();
101 if (this->size_ == 0) {
102 this->_is_empty.store(
true, std::memory_order_release);
115 std::lock_guard<std::mutex>
lock{ this->_lock };
116 if (this->size_ == 0) {
120 item = this->batches_.front()[this->front_index_];
132 std::lock_guard<std::mutex>
lock{ this->_lock };
141 if (this->size_ == 0) {
142 this->_is_empty.store(
true, std::memory_order_release);
156 template<
typename Callback>
158 std::vector<T> snapshot;
160 std::lock_guard<std::mutex>
lock{ this->_lock };
161 snapshot.reserve(this->size_);
164 for (
const auto&
batch : this->batches_) {
165 const size_t start =
first_batch ? this->front_index_ : 0;
168 for (
size_t i = start; i <
batch.size(); ++i) {
169 snapshot.push_back(
batch[i]);
174 std::forward<Callback>(
callback)(snapshot);
179 return this->_is_waitable.load(std::memory_order_acquire);
187 std::unique_lock<std::mutex> lock{ this->_lock };
188 this->_cond.wait(lock, [
this] {
return this->size_ >= _notify_size || !this->
is_waitable(); });
192 size_t size() const noexcept {
193 std::lock_guard<std::mutex> lock{ this->_lock };
199 std::lock_guard<std::mutex>
lock{ this->_lock };
200 this->_is_waitable.store(
true, std::memory_order_release);
201 this->_cond.notify_all();
205 std::lock_guard<std::mutex>
lock{ this->_lock };
206 this->_is_waitable.store(
false, std::memory_order_release);
207 this->_cond.notify_all();
211 std::atomic<bool> _is_empty{
true };
212 std::atomic<bool> _is_waitable{
false };
214 mutable std::mutex _lock;
215 std::condition_variable _cond;
216 std::deque<std::vector<T>> batches_;
217 size_t front_index_ = 0;
220 void discard_exhausted_front_batch() noexcept {
221 while (!this->batches_.empty() && this->front_index_ >= this->batches_.front().size()) {
222 this->batches_.pop_front();
223 this->front_index_ = 0;