From 6c081144e5bcf31edece0806c38d63c97257cc4c Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 26 Jan 2018 14:32:06 -0800 Subject: ThreadPool refactoring (#1389) Refactor ThreadPool code for clarity and to fix some bugs with using the pool from different threads in parallel. We have a singleton pool, and need to ensure it is created only once and used only by one thread at a time. This model is a simple way to ensure we use a number of threads equal to the number of cores, more or less (a pool per Module might lead to number of cores * number of Modules being optimized). This refactoring adds a parent pointer in the worker threads (giving them direct access to the pool makes it simpler to make sure that pool and thread creation and teardown are threadsafe). This commit also adds proper locking around pool creation and pool usage. --- src/support/threads.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/support/threads.h') diff --git a/src/support/threads.h b/src/support/threads.h index 0ec109e4d..280e19470 100644 --- a/src/support/threads.h +++ b/src/support/threads.h @@ -38,6 +38,8 @@ enum class ThreadWorkState { Finished }; +class ThreadPool; + // // A helper thread. // @@ -45,6 +47,7 @@ enum class ThreadWorkState { // class Thread { + ThreadPool* parent; std::unique_ptr thread; std::mutex mutex; std::condition_variable condition; @@ -52,7 +55,7 @@ class Thread { std::function doWork = nullptr; public: - Thread(); + Thread(ThreadPool* parent); ~Thread(); // Start to do work, calling doWork() until @@ -72,10 +75,17 @@ private: class ThreadPool { std::vector> threads; bool running = false; - std::mutex mutex; std::condition_variable condition; std::atomic ready; + // A mutex for creating the pool safely + static std::mutex creationMutex; + // A mutex for work() so that the pool can only work on one + // thing at a time + static std::mutex workMutex; + // A mutex for communication with the worker threads + static std::mutex threadMutex; + private: void initialize(size_t num); -- cgit v1.2.3