diff --git a/llvm/lib/Support/Parallel.cpp b/llvm/lib/Support/Parallel.cpp index c256d256be4fa1..f762e7abc83393 100644 --- a/llvm/lib/Support/Parallel.cpp +++ b/llvm/lib/Support/Parallel.cpp @@ -55,7 +55,10 @@ class ThreadPoolExecutor : public Executor { Threads.reserve(ThreadCount); Threads.resize(1); std::lock_guard Lock(Mutex); - Threads[0] = std::thread([this, ThreadCount, S] { + // Use operator[] before creating the thread to avoid data race in .size() + // in “safe libc++” mode. + auto& Thread0 = Threads[0]; + Thread0 = std::thread([this, ThreadCount, S] { for (unsigned I = 1; I < ThreadCount; ++I) { Threads.emplace_back([=] { work(S, I); }); if (Stop)