diff --git a/sycl/source/detail/thread_pool.hpp b/sycl/source/detail/thread_pool.hpp index 304045389b53..50240e0a98b0 100644 --- a/sycl/source/detail/thread_pool.hpp +++ b/sycl/source/detail/thread_pool.hpp @@ -29,17 +29,17 @@ class ThreadPool { std::queue> MJobQueue; std::mutex MJobQueueMutex; std::condition_variable MDoSmthOrStop; - std::atomic_bool MStop; + bool MStop = false; std::atomic_uint MJobsInPool; void worker() { GlobalHandler::instance().registerSchedulerUsage(/*ModifyCounter*/ false); std::unique_lock Lock(MJobQueueMutex); while (true) { - MDoSmthOrStop.wait( - Lock, [this]() { return !MJobQueue.empty() || MStop.load(); }); + MDoSmthOrStop.wait(Lock, + [this]() { return !MJobQueue.empty() || MStop; }); - if (MStop.load()) + if (MStop) break; std::function Job = std::move(MJobQueue.front()); @@ -57,7 +57,6 @@ class ThreadPool { void start() { MLaunchedThreads.reserve(MThreadCount); - MStop.store(false); MJobsInPool.store(0); for (size_t Idx = 0; Idx < MThreadCount; ++Idx) @@ -83,7 +82,10 @@ class ThreadPool { } void finishAndWait() { - MStop.store(true); + { + std::lock_guard Lock(MJobQueueMutex); + MStop = true; + } MDoSmthOrStop.notify_all(); diff --git a/sycl/test-e2e/Regression/unwaited_for_host_task.cpp b/sycl/test-e2e/Regression/unwaited_for_host_task.cpp new file mode 100644 index 000000000000..6f6ae3090c1e --- /dev/null +++ b/sycl/test-e2e/Regression/unwaited_for_host_task.cpp @@ -0,0 +1,16 @@ +// RUN: %{build} -o %t.out +// RUN: %{run} %t.out + +#include + +using namespace sycl; + +// This test checks that host tasks that haven't been waited for do not cause +// issues during shutdown. +int main(int argc, char *argv[]) { + queue q; + + q.submit([&](handler &h) { h.host_task([=]() {}); }); + + return 0; +}