Browse Source

Made ThreadPool a singleton

Paul-Louis Ageneau 5 years ago
parent
commit
d60e18d963
2 changed files with 9 additions and 2 deletions
  1. 5 0
      src/threadpool.cpp
  2. 4 2
      src/threadpool.hpp

+ 5 - 0
src/threadpool.cpp

@@ -20,6 +20,11 @@
 
 namespace rtc {
 
+ThreadPool &ThreadPool::Instance() {
+	static ThreadPool instance;
+	return instance;
+}
+
 ThreadPool::ThreadPool(int count) { spawn(count); }
 
 ThreadPool::~ThreadPool() { join(); }

+ 4 - 2
src/threadpool.hpp

@@ -37,12 +37,11 @@ using invoke_future_t = std::future<std::invoke_result_t<std::decay_t<F>, std::d
 
 class ThreadPool final {
 public:
-	explicit ThreadPool(int count);
+	static ThreadPool &Instance();
 	ThreadPool(const ThreadPool &) = delete;
 	ThreadPool &operator=(const ThreadPool &) = delete;
 	ThreadPool(ThreadPool &&) = delete;
 	ThreadPool &operator=(ThreadPool &&) = delete;
-	~ThreadPool();
 
 	int count() const;
 	void spawn(int count = 1);
@@ -54,6 +53,9 @@ public:
 	auto enqueue(F &&f, Args &&... args) -> invoke_future_t<F, Args...>;
 
 protected:
+	explicit ThreadPool(int count = 0);
+	~ThreadPool();
+
 	std::function<void()> dequeue(); // returns null function if joining
 
 	std::vector<std::thread> mWorkers;