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 + } }