Index: lib/Support/ThreadPool.cpp =================================================================== --- lib/Support/ThreadPool.cpp +++ lib/Support/ThreadPool.cpp @@ -75,8 +75,11 @@ void ThreadPool::wait() { // Wait for all threads to complete and the queue to be empty std::unique_lock LockGuard(CompletionLock); + // The order of the checks for ActiveThreads and Tasks.empty() matters because + // any active threads might be modifying the Tasks queue, and this would be a + // race. CompletionCondition.wait(LockGuard, - [&] { return Tasks.empty() && !ActiveThreads; }); + [&] { return !ActiveThreads && Tasks.empty(); }); } std::shared_future ThreadPool::asyncImpl(TaskTy Task) {