/* #define DEBUGTRACE_ENABLED */ #include "lasp_thread.h" #include "BS_thread_pool.hpp" #include "debugtrace.hpp" #include /** * @brief Store a global weak_ptr, that is used to create new shared pointers * if any other shared pointers are still alive. If not, we create a new * instance. */ std::weak_ptr _global_weak_pool; /** * @brief Global mutex, used to restrict the pool creation to a single thread * at once. */ std::mutex _mtx; using Lck = std::scoped_lock; using rte = std::runtime_error; GlobalThreadPool::GlobalThreadPool() { DEBUGTRACE_ENTER; Lck lck(_mtx); /// See if we can get it from the global ptr. If not, time to allocate it. _pool = _global_weak_pool.lock(); if (!_pool) { _pool = std::make_shared(); if (!_pool) { throw rte("Fatal: could not allocate thread pool!"); } // Update global weak pointer _global_weak_pool = _pool; } }