From 83112f63aec6213f50957f78998ca51a26e088cc Mon Sep 17 00:00:00 2001 From: Laura Canalini Date: Tue, 8 Mar 2022 10:28:21 +0100 Subject: [PATCH] Fix bug that blocks the data loader when not all batches in the queue are consumed --- modules/eddl/include/ecvl/support_eddl.h | 13 +++++++++++++ modules/eddl/src/support_eddl.cpp | 7 ++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/modules/eddl/include/ecvl/support_eddl.h b/modules/eddl/include/ecvl/support_eddl.h index 6ca1ff59..2326cd59 100644 --- a/modules/eddl/include/ecvl/support_eddl.h +++ b/modules/eddl/include/ecvl/support_eddl.h @@ -177,6 +177,19 @@ class ProducersConsumerQueue cond_notempty_.notify_one(); } + /** @brief Free threads locked on a push operation. + + Notifies all the locked threads on the condition variable cond_notfull_ that were waiting to retake the control in the + critical region of pushing new elements in to the queue. + + This method is specific to be used in the Stop() operation when the data loading process needs to be stopped before all + the elements (batches) of the queue have been consumed. + */ + void FreeLockedOnPush() + { + cond_notfull_.notify_all(); + } + /** @brief Pop a sample from the queue. Take the lock of the queue and wait if the queue is empty. Otherwise, pop a sample, an image and its label from the queue. diff --git a/modules/eddl/src/support_eddl.cpp b/modules/eddl/src/support_eddl.cpp index 057106de..6710d847 100644 --- a/modules/eddl/src/support_eddl.cpp +++ b/modules/eddl/src/support_eddl.cpp @@ -518,7 +518,12 @@ void DLDataset::Stop() ECVL_ERROR_STOP_ALREADY_END } - active_ = false; + { + std::unique_lock lock(active_mutex_); + active_ = false; + queue_.FreeLockedOnPush(); + } + for (auto i = 0u; i < num_workers_; ++i) { producers_[i].join(); }