From febbe14e6ddb92a6d80d2393cced4d85901c7f93 Mon Sep 17 00:00:00 2001 From: seelabs Date: Tue, 11 Jan 2022 13:56:07 -0500 Subject: [PATCH] Adjust mutex scope in `walkMapParallel`: This commit corrects a technical flaw that was introduced with commit 7c12f0135897361398917ad2c8cda888249d42ae: 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. --- src/ripple/shamap/impl/SHAMapDelta.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ripple/shamap/impl/SHAMapDelta.cpp b/src/ripple/shamap/impl/SHAMapDelta.cpp index 13f28ddf96..eb6da19e3f 100644 --- a/src/ripple/shamap/impl/SHAMapDelta.cpp +++ b/src/ripple/shamap/impl/SHAMapDelta.cpp @@ -314,6 +314,10 @@ SHAMap::walkMapParallel( std::array>, 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(child)); JLOG(journal_.debug()) << "starting worker " << rootChildIndex; - std::mutex m; workers.push_back(std::thread( [&m, &missingNodes, &maxMissing, this]( std::stack> nodeStack) {