diff --git a/src/threadpool.cpp b/src/threadpool.cpp index f43ee88..3105fb2 100644 --- a/src/threadpool.cpp +++ b/src/threadpool.cpp @@ -18,15 +18,26 @@ #include "threadpool.hpp" +#include + +namespace { + void joinThreadPoolInstance() { + rtc::ThreadPool::Instance().join(); + } +} + namespace rtc { ThreadPool &ThreadPool::Instance() { - // Init handles joining on cleanup static ThreadPool *instance = new ThreadPool; return *instance; } -ThreadPool::~ThreadPool() { join(); } +ThreadPool::ThreadPool() { + std::atexit(joinThreadPoolInstance); +} + +ThreadPool::~ThreadPool() {} int ThreadPool::count() const { std::unique_lock lock(mWorkersMutex); diff --git a/src/threadpool.hpp b/src/threadpool.hpp index 6df8cfd..d554747 100644 --- a/src/threadpool.hpp +++ b/src/threadpool.hpp @@ -56,7 +56,7 @@ public: auto enqueue(F &&f, Args &&... args) -> invoke_future_t; protected: - ThreadPool() = default; + ThreadPool(); ~ThreadPool(); std::function dequeue(); // returns null function if joining