From 23288b7cb51902262dc3c8e608e3c04da5c46a03 Mon Sep 17 00:00:00 2001 From: Nikita Shulga Date: Mon, 7 Oct 2019 10:58:25 -0700 Subject: [PATCH] Use atomic operations to modify flagNestedParallelFor This ensures uniform behavior on any C++11 compliant compiler --- modules/core/src/parallel.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/modules/core/src/parallel.cpp b/modules/core/src/parallel.cpp index 5b1c917..f4a0a75 100644 --- a/modules/core/src/parallel.cpp +++ b/modules/core/src/parallel.cpp @@ -134,6 +134,10 @@ # define CV_PARALLEL_FRAMEWORK "pthreads" #endif +#ifdef CV_PARALLEL_FRAMEWORK +#include +#endif + #include "parallel_impl.hpp" #include "opencv2/core/detail/exception_ptr.hpp" // CV__EXCEPTION_PTR = 1 if std::exception_ptr is available @@ -487,20 +491,20 @@ void cv::parallel_for_(const cv::Range& range, const cv::ParallelLoopBody& body, return; #ifdef CV_PARALLEL_FRAMEWORK - static volatile int flagNestedParallelFor = 0; - bool isNotNestedRegion = flagNestedParallelFor == 0; + static std::atomic flagNestedParallelFor(false); + bool isNotNestedRegion = !flagNestedParallelFor.load(); if (isNotNestedRegion) - isNotNestedRegion = CV_XADD(&flagNestedParallelFor, 1) == 0; + isNotNestedRegion = !flagNestedParallelFor.exchange(true); if (isNotNestedRegion) { try { parallel_for_impl(range, body, nstripes); - flagNestedParallelFor = 0; + flagNestedParallelFor = false; } catch (...) { - flagNestedParallelFor = 0; + flagNestedParallelFor = false; throw; } } -- 2.7.4