Commit 23288b7c authored by Nikita Shulga's avatar Nikita Shulga

Use atomic operations to modify flagNestedParallelFor

This ensures uniform behavior on any C++11 compliant compiler
parent 626bfbf3
...@@ -134,6 +134,10 @@ ...@@ -134,6 +134,10 @@
# define CV_PARALLEL_FRAMEWORK "pthreads" # define CV_PARALLEL_FRAMEWORK "pthreads"
#endif #endif
#ifdef CV_PARALLEL_FRAMEWORK
#include <atomic>
#endif
#include "parallel_impl.hpp" #include "parallel_impl.hpp"
#include "opencv2/core/detail/exception_ptr.hpp" // CV__EXCEPTION_PTR = 1 if std::exception_ptr is available #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, ...@@ -487,20 +491,20 @@ void cv::parallel_for_(const cv::Range& range, const cv::ParallelLoopBody& body,
return; return;
#ifdef CV_PARALLEL_FRAMEWORK #ifdef CV_PARALLEL_FRAMEWORK
static volatile int flagNestedParallelFor = 0; static std::atomic<bool> flagNestedParallelFor(false);
bool isNotNestedRegion = flagNestedParallelFor == 0; bool isNotNestedRegion = !flagNestedParallelFor.load();
if (isNotNestedRegion) if (isNotNestedRegion)
isNotNestedRegion = CV_XADD(&flagNestedParallelFor, 1) == 0; isNotNestedRegion = !flagNestedParallelFor.exchange(true);
if (isNotNestedRegion) if (isNotNestedRegion)
{ {
try try
{ {
parallel_for_impl(range, body, nstripes); parallel_for_impl(range, body, nstripes);
flagNestedParallelFor = 0; flagNestedParallelFor = false;
} }
catch (...) catch (...)
{ {
flagNestedParallelFor = 0; flagNestedParallelFor = false;
throw; throw;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment