Adjust mutex scope in walkMapParallel:

This commit corrects a technical flaw that was introduced with commit
7c12f01358: as written, a mutex that is
intended to help provide synchronization for multiple threads as they
are each walking the map, is declared so that each thread is passed a
dangling reference to a unique mutex.

This commit hoists the mutex outside the thread creation loop, so all
threads use a single mutex and eliminating the dangling reference.
This commit is contained in:
seelabs
2022-01-11 13:56:07 -05:00
committed by Nik Bougalis
parent 44514930f9
commit febbe14e6d

View File

@@ -314,6 +314,10 @@ SHAMap::walkMapParallel(
std::array<std::stack<StackEntry, std::vector<StackEntry>>, 16> nodeStacks;
// This mutex is used inside the worker threads to protect `missingNodes`
// and `maxMissing` from race conditions
std::mutex m;
for (int rootChildIndex = 0; rootChildIndex < 16; ++rootChildIndex)
{
auto const& child = topChildren[rootChildIndex];
@@ -324,7 +328,6 @@ SHAMap::walkMapParallel(
std::static_pointer_cast<SHAMapInnerNode>(child));
JLOG(journal_.debug()) << "starting worker " << rootChildIndex;
std::mutex m;
workers.push_back(std::thread(
[&m, &missingNodes, &maxMissing, this](
std::stack<StackEntry, std::vector<StackEntry>> nodeStack) {