diff --git a/llvm/include/llvm/Support/Parallel.h b/llvm/include/llvm/Support/Parallel.h --- a/llvm/include/llvm/Support/Parallel.h +++ b/llvm/include/llvm/Support/Parallel.h @@ -48,8 +48,13 @@ } void dec() { - std::lock_guard lock(Mutex); - if (--Count == 0) + bool Zero = false; + { + std::lock_guard lock(Mutex); + if (--Count == 0) + Zero = true; + } + if (Zero) Cond.notify_all(); } diff --git a/llvm/lib/Support/Parallel.cpp b/llvm/lib/Support/Parallel.cpp --- a/llvm/lib/Support/Parallel.cpp +++ b/llvm/lib/Support/Parallel.cpp @@ -151,7 +151,10 @@ // lock, only allow the first TaskGroup to run tasks parallelly. In the scenario // of nested parallel_for_each(), only the outermost one runs parallelly. TaskGroup::TaskGroup() : Parallel(TaskGroupInstances++ == 0) {} -TaskGroup::~TaskGroup() { --TaskGroupInstances; } +TaskGroup::~TaskGroup() { + L.sync(); + --TaskGroupInstances; +} void TaskGroup::spawn(std::function F) { if (Parallel) {