feat: ThreadPool refac & dynamic thread management

This commit is contained in:
Florian Sylvain
2025-05-14 00:53:24 +02:00
parent 60c33a7965
commit 6969cacacc
4 changed files with 82 additions and 46 deletions
+7 -3
View File
@@ -164,10 +164,14 @@ void Game::update() {
}
void Game::updateBallsParallel(float dt) {
const size_t chunkSize =
std::max(static_cast<size_t>(1),
m_objects.size() / std::thread::hardware_concurrency());
const size_t optimalThreads =
ThreadUtils::calculateOptimalThreads(m_objects.size());
if (m_threadPool.getThreadCount() != optimalThreads) {
m_threadPool.resize(optimalThreads);
}
const size_t chunkSize =
std::max(static_cast<size_t>(1), m_objects.size() / optimalThreads);
std::vector<std::future<void>> futures;
for (size_t i = 0; i < m_objects.size(); i += chunkSize) {
+51
View File
@@ -0,0 +1,51 @@
#include "ThreadPool.hpp"
ThreadPool::ThreadPool(const size_t numThreads) : stop(false) {
startWorkers(numThreads);
}
ThreadPool::~ThreadPool() { stopAllWorkers(); }
void ThreadPool::resize(const size_t newSize) {
if (newSize == workers.size()) return;
stopAllWorkers();
workers.clear();
stop = false;
startWorkers(newSize);
}
void ThreadPool::stopAllWorkers() {
{
std::unique_lock lock(queueMutex);
stop = true;
}
condition.notify_all();
for (auto& worker : workers) {
if (worker.joinable()) {
worker.join();
}
}
}
void ThreadPool::startWorkers(const size_t numWorkers) {
for (size_t i = 0; i < numWorkers; ++i) {
workers.emplace_back([this] {
while (true) {
std::function<void()> task;
{
std::unique_lock lock(this->queueMutex);
this->condition.wait(
lock, [this] { return this->stop || !this->tasks.empty(); });
if (this->stop && this->tasks.empty()) return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
});
}
}
+13 -42
View File
@@ -1,3 +1,4 @@
#pragma once
#include <condition_variable>
#include <functional>
@@ -10,63 +11,33 @@
class ThreadPool {
public:
explicit ThreadPool(size_t numThreads);
~ThreadPool();
[[nodiscard]] size_t getThreadCount() const { return workers.size(); }
void resize(size_t newSize);
template <class F, class... Args>
auto enqueue(F &&f, Args &&...args)
-> std::future<std::invoke_result_t<F, Args...> >;
auto enqueue(F&& f, Args&&... args)
-> std::future<std::invoke_result_t<F, Args...>>;
private:
void stopAllWorkers();
void startWorkers(size_t numWorkers);
std::vector<std::thread> workers;
std::queue<std::function<void()> > tasks;
std::queue<std::function<void()>> tasks;
std::mutex queueMutex;
std::condition_variable condition;
bool stop;
bool stop{false};
};
inline ThreadPool::ThreadPool(const size_t numThreads) : stop(false) {
for (size_t i = 0; i < numThreads; ++i) {
workers.emplace_back([this] {
while (true) {
std::function<void()> task;
{
std::unique_lock lock(this->queueMutex);
this->condition.wait(
lock, [this] { return this->stop || !this->tasks.empty(); });
if (this->stop && this->tasks.empty()) return;
task = std::move(this->tasks.front());
this->tasks.pop();
}
task();
}
});
}
}
inline ThreadPool::~ThreadPool() {
{
std::unique_lock lock(queueMutex);
stop = true;
}
condition.notify_all();
for (auto &worker : workers) worker.join();
}
template <class F, class... Args>
auto ThreadPool::enqueue(F &&f, Args &&...args)
-> std::future<std::invoke_result_t<F, Args...> > {
auto ThreadPool::enqueue(F&& f, Args&&... args)
-> std::future<std::invoke_result_t<F, Args...>> {
using return_type = std::invoke_result_t<F, Args...>;
auto task = std::make_shared<std::packaged_task<return_type()> >(
auto task = std::make_shared<std::packaged_task<return_type()>>(
std::bind(std::forward<F>(f), std::forward<Args>(args)...));
std::future<return_type> res = task->get_future();
+10
View File
@@ -1,3 +1,4 @@
#pragma once
#include <thread>
namespace ThreadUtils {
@@ -5,4 +6,13 @@ inline size_t calculateSafeWorkerThreads(const size_t reserve = 2) {
const size_t total = std::thread::hardware_concurrency();
return total > reserve ? total - reserve : 1;
}
inline size_t calculateOptimalThreads(const size_t objectCount,
const size_t minObjectsPerThread = 100) {
const size_t maxThreads = calculateSafeWorkerThreads();
const size_t optimalThreads =
std::max(static_cast<size_t>(1), objectCount / minObjectsPerThread);
return std::min(maxThreads, optimalThreads);
}
} // namespace ThreadUtils