Skip to content

Commit 3335e69

Browse files
N-Dekkerdzenanz
authored andcommitted
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 #4168 commit 1094cdd "STYLE: Replace `std::unique_lock` with `std::lock_guard` in ThreadPool"
1 parent ff64d3c commit 3335e69

File tree

4 files changed

+50
-66
lines changed

4 files changed

+50
-66
lines changed

Modules/Filtering/ImageStatistics/include/itkLabelOverlapMeasuresImageFilter.hxx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,28 +120,23 @@ LabelOverlapMeasuresImageFilter<TLabelImage>::ThreadedStreamedGenerateData(const
120120
// local copy, this thread may do multiple merges.
121121
while (true)
122122
{
123-
123+
MapType tomerge{};
124124
{
125-
std::unique_lock<std::mutex> lock(m_Mutex);
125+
const std::lock_guard<std::mutex> lockGuard(m_Mutex);
126126

127127
if (m_LabelSetMeasures.empty())
128128
{
129129
swap(m_LabelSetMeasures, localStatistics);
130130
break;
131131
}
132-
else
133-
{
134-
// copy the output map to thread local storage
135-
MapType tomerge;
136-
swap(m_LabelSetMeasures, tomerge);
137132

138-
// allow other threads to merge data
139-
lock.unlock();
133+
// Move the data of the output map to the local `tomerge` and clear the output map.
134+
swap(m_LabelSetMeasures, tomerge);
140135

141-
// Merge tomerge into localStatistics, locally
142-
MergeMap(localStatistics, tomerge);
143-
}
144-
} // release lock
136+
} // release lock, allow other threads to merge data
137+
138+
// Merge tomerge into localStatistics, locally
139+
MergeMap(localStatistics, tomerge);
145140
}
146141
}
147142

Modules/Filtering/ImageStatistics/include/itkLabelStatisticsImageFilter.hxx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -257,28 +257,23 @@ LabelStatisticsImageFilter<TInputImage, TLabelImage>::ThreadedStreamedGenerateDa
257257
// local copy, this thread may do multiple merges.
258258
while (true)
259259
{
260-
260+
MapType tomerge{};
261261
{
262-
std::unique_lock<std::mutex> lock(m_Mutex);
262+
const std::lock_guard<std::mutex> lockGuard(m_Mutex);
263263

264264
if (m_LabelStatistics.empty())
265265
{
266266
swap(m_LabelStatistics, localStatistics);
267267
break;
268268
}
269-
else
270-
{
271-
// copy the output map to thread local storage
272-
MapType tomerge;
273-
swap(m_LabelStatistics, tomerge);
274269

275-
// allow other threads to merge data
276-
lock.unlock();
270+
// Move the data of the output map to the local `tomerge` and clear the output map.
271+
swap(m_LabelStatistics, tomerge);
277272

278-
// Merge tomerge into localStatistics, locally
279-
MergeMap(localStatistics, tomerge);
280-
}
281-
} // release lock
273+
} // release lock, allow other threads to merge data
274+
275+
// Merge tomerge into localStatistics, locally
276+
MergeMap(localStatistics, tomerge);
282277
}
283278
}
284279

Modules/Numerics/Statistics/include/itkImageToHistogramFilter.hxx

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -263,39 +263,35 @@ ImageToHistogramFilter<TImage>::ThreadedMergeHistogram(HistogramPointer && histo
263263
{
264264
while (true)
265265
{
266-
267-
std::unique_lock<std::mutex> lock(m_Mutex);
268-
269-
if (m_MergeHistogram.IsNull())
270-
{
271-
m_MergeHistogram = std::move(histogram);
272-
return;
273-
}
274-
else
266+
HistogramPointer tomergeHistogram{};
275267
{
268+
const std::lock_guard<std::mutex> lockGuard(m_Mutex);
269+
270+
if (m_MergeHistogram.IsNull())
271+
{
272+
m_MergeHistogram = std::move(histogram);
273+
return;
274+
}
276275

277276
// merge/reduce the local results with current values in m_MergeHistogram
278277

279278
// take ownership locally
280-
HistogramPointer tomergeHistogram;
281279
swap(m_MergeHistogram, tomergeHistogram);
282280

283-
// allow other threads to merge data
284-
lock.unlock();
281+
} // release lock, allow other threads to merge data
285282

286-
using HistogramIterator = typename HistogramType::ConstIterator;
283+
using HistogramIterator = typename HistogramType::ConstIterator;
287284

288-
HistogramIterator hit = tomergeHistogram->Begin();
289-
HistogramIterator end = tomergeHistogram->End();
285+
HistogramIterator hit = tomergeHistogram->Begin();
286+
HistogramIterator end = tomergeHistogram->End();
290287

291-
typename HistogramType::IndexType index;
288+
typename HistogramType::IndexType index;
292289

293-
while (hit != end)
294-
{
295-
histogram->GetIndex(hit.GetMeasurementVector(), index);
296-
histogram->IncreaseFrequencyOfIndex(index, hit.GetFrequency());
297-
++hit;
298-
}
290+
while (hit != end)
291+
{
292+
histogram->GetIndex(hit.GetMeasurementVector(), index);
293+
histogram->IncreaseFrequencyOfIndex(index, hit.GetFrequency());
294+
++hit;
299295
}
300296
}
301297
}

Modules/Segmentation/ConnectedComponents/include/itkRelabelComponentImageFilter.hxx

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,29 +96,27 @@ RelabelComponentImageFilter<TInputImage, TOutputImage>::ParallelComputeLabels(co
9696
// local copy, this thread may do multiple merges.
9797
while (true)
9898
{
99-
std::unique_lock<std::mutex> lock(m_Mutex);
100-
101-
if (m_SizeMap.empty())
102-
{
103-
swap(m_SizeMap, localSizeMap);
104-
break;
105-
}
106-
else
99+
MapType toMerge{};
107100
{
108-
// copy the output map to thread local storage
109-
MapType toMerge;
110-
swap(m_SizeMap, toMerge);
111-
112-
// allow other threads to merge data
113-
lock.unlock();
101+
const std::lock_guard<std::mutex> lockGuard(m_Mutex);
114102

115-
// Merge toMerge into localSizeMap, locally
116-
for (auto & sizePair : toMerge)
103+
if (m_SizeMap.empty())
117104
{
118-
localSizeMap[sizePair.first] += sizePair.second;
105+
swap(m_SizeMap, localSizeMap);
106+
break;
119107
}
108+
109+
// Move the data of the output map to the local `toMerge` and clear the output map.
110+
swap(m_SizeMap, toMerge);
111+
112+
} // release lock, allow other threads to merge data
113+
114+
// Merge toMerge into localSizeMap, locally
115+
for (auto & sizePair : toMerge)
116+
{
117+
localSizeMap[sizePair.first] += sizePair.second;
120118
}
121-
} // release lock
119+
}
122120
}
123121

124122

0 commit comments

Comments
 (0)