From 227f4eeeeac6ce741fca0cac310d6310ba4f4920 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Wed, 23 Aug 2023 19:05:16 +0200 Subject: [PATCH] STYLE: Replace std::unique_lock with std::lock_guard in Filter.hxx files Removed explicit `unlock()` calls. Instead, just made sure that the `lock_guard` variables go out of scope as soon as other threads are allowed to merge data. Follow-up to pull request https://github.com/InsightSoftwareConsortium/ITK/pull/4168 commit 1094cdde9ae725ad257d5d77308fffaa3dd6e0fe "STYLE: Replace `std::unique_lock` with `std::lock_guard` in ThreadPool" --- .../itkLabelOverlapMeasuresImageFilter.hxx | 21 +++++------- .../include/itkLabelStatisticsImageFilter.hxx | 21 +++++------- .../itkRelabelComponentImageFilter.hxx | 34 +++++++++---------- 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/Modules/Filtering/ImageStatistics/include/itkLabelOverlapMeasuresImageFilter.hxx b/Modules/Filtering/ImageStatistics/include/itkLabelOverlapMeasuresImageFilter.hxx index f5ee24198e6..afe2affa553 100644 --- a/Modules/Filtering/ImageStatistics/include/itkLabelOverlapMeasuresImageFilter.hxx +++ b/Modules/Filtering/ImageStatistics/include/itkLabelOverlapMeasuresImageFilter.hxx @@ -120,28 +120,23 @@ LabelOverlapMeasuresImageFilter::ThreadedStreamedGenerateData(const // local copy, this thread may do multiple merges. while (true) { - + MapType tomerge{}; { - std::unique_lock lock(m_Mutex); + const std::lock_guard lockGuard(m_Mutex); if (m_LabelSetMeasures.empty()) { swap(m_LabelSetMeasures, localStatistics); break; } - else - { - // copy the output map to thread local storage - MapType tomerge; - swap(m_LabelSetMeasures, tomerge); - // allow other threads to merge data - lock.unlock(); + // Move the data of the output map to the local `tomerge` and clear the output map. + swap(m_LabelSetMeasures, tomerge); - // Merge tomerge into localStatistics, locally - MergeMap(localStatistics, tomerge); - } - } // release lock + } // release lock, allow other threads to merge data + + // Merge tomerge into localStatistics, locally + MergeMap(localStatistics, tomerge); } } diff --git a/Modules/Filtering/ImageStatistics/include/itkLabelStatisticsImageFilter.hxx b/Modules/Filtering/ImageStatistics/include/itkLabelStatisticsImageFilter.hxx index 2283817b31a..807dd227652 100644 --- a/Modules/Filtering/ImageStatistics/include/itkLabelStatisticsImageFilter.hxx +++ b/Modules/Filtering/ImageStatistics/include/itkLabelStatisticsImageFilter.hxx @@ -257,28 +257,23 @@ LabelStatisticsImageFilter::ThreadedStreamedGenerateDa // local copy, this thread may do multiple merges. while (true) { - + MapType tomerge{}; { - std::unique_lock lock(m_Mutex); + const std::lock_guard lockGuard(m_Mutex); if (m_LabelStatistics.empty()) { swap(m_LabelStatistics, localStatistics); break; } - else - { - // copy the output map to thread local storage - MapType tomerge; - swap(m_LabelStatistics, tomerge); - // allow other threads to merge data - lock.unlock(); + // Move the data of the output map to the local `tomerge` and clear the output map. + swap(m_LabelStatistics, tomerge); - // Merge tomerge into localStatistics, locally - MergeMap(localStatistics, tomerge); - } - } // release lock + } // release lock, allow other threads to merge data + + // Merge tomerge into localStatistics, locally + MergeMap(localStatistics, tomerge); } } diff --git a/Modules/Segmentation/ConnectedComponents/include/itkRelabelComponentImageFilter.hxx b/Modules/Segmentation/ConnectedComponents/include/itkRelabelComponentImageFilter.hxx index 4b85037a920..c7888856d92 100644 --- a/Modules/Segmentation/ConnectedComponents/include/itkRelabelComponentImageFilter.hxx +++ b/Modules/Segmentation/ConnectedComponents/include/itkRelabelComponentImageFilter.hxx @@ -96,29 +96,27 @@ RelabelComponentImageFilter::ParallelComputeLabels(co // local copy, this thread may do multiple merges. while (true) { - std::unique_lock lock(m_Mutex); - - if (m_SizeMap.empty()) - { - swap(m_SizeMap, localSizeMap); - break; - } - else + MapType toMerge{}; { - // copy the output map to thread local storage - MapType toMerge; - swap(m_SizeMap, toMerge); - - // allow other threads to merge data - lock.unlock(); + const std::lock_guard lockGuard(m_Mutex); - // Merge toMerge into localSizeMap, locally - for (auto & sizePair : toMerge) + if (m_SizeMap.empty()) { - localSizeMap[sizePair.first] += sizePair.second; + swap(m_SizeMap, localSizeMap); + break; } + + // Move the data of the output map to the local `toMerge` and clear the output map. + swap(m_SizeMap, toMerge); + + } // release lock, allow other threads to merge data + + // Merge toMerge into localSizeMap, locally + for (auto & sizePair : toMerge) + { + localSizeMap[sizePair.first] += sizePair.second; } - } // release lock + } }