From 1d591d76756a0b3b020b6f5d37dcdbce3b954226 Mon Sep 17 00:00:00 2001 From: Eugene Sorokin Date: Mon, 17 Oct 2022 16:49:02 +0300 Subject: [PATCH] Fixed possible memory order bug --- BS_thread_pool.hpp | 9 +++++++-- BS_thread_pool_light.hpp | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/BS_thread_pool.hpp b/BS_thread_pool.hpp index 840bcdc..c01bd8a 100644 --- a/BS_thread_pool.hpp +++ b/BS_thread_pool.hpp @@ -429,6 +429,8 @@ class [[nodiscard]] thread_pool const std::scoped_lock tasks_lock(tasks_mutex); tasks.push(task_function); } + + const std::lock_guard tasks_lock(tasks_mutex); ++tasks_total; task_available_cv.notify_one(); } @@ -535,8 +537,11 @@ class [[nodiscard]] thread_pool */ void destroy_threads() { - running = false; - task_available_cv.notify_all(); + { + const std::lock_guard tasks_locker(tasks_mutex); + running = false; + task_available_cv.notify_all(); + } for (concurrency_t i = 0; i < thread_count; ++i) { threads[i].join(); diff --git a/BS_thread_pool_light.hpp b/BS_thread_pool_light.hpp index b432b9f..695f8c4 100644 --- a/BS_thread_pool_light.hpp +++ b/BS_thread_pool_light.hpp @@ -140,6 +140,8 @@ class [[nodiscard]] thread_pool_light const std::scoped_lock tasks_lock(tasks_mutex); tasks.push(task_function); } + + const std::lock_guard tasks_lock(tasks_mutex); ++tasks_total; task_available_cv.notify_one(); } @@ -221,8 +223,11 @@ class [[nodiscard]] thread_pool_light */ void destroy_threads() { - running = false; - task_available_cv.notify_all(); + { + const std::lock_guard tasks_locker( tasks_mutex ); + running = false; + task_available_cv.notify_all(); + } for (concurrency_t i = 0; i < thread_count; ++i) { threads[i].join();