Join thread pool at exit to prevent static destruction order issue

This commit is contained in:
Paul-Louis Ageneau
2021-01-21 12:07:50 +01:00
parent b79c886480
commit 4e3ea69073
2 changed files with 14 additions and 3 deletions

View File

@ -18,15 +18,26 @@
#include "threadpool.hpp"
#include <cstdlib>
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);

View File

@ -56,7 +56,7 @@ public:
auto enqueue(F &&f, Args &&... args) -> invoke_future_t<F, Args...>;
protected:
ThreadPool() = default;
ThreadPool();
~ThreadPool();
std::function<void()> dequeue(); // returns null function if joining