mirror of
https://github.com/Floriansylvain/SFMLplayground.git
synced 2026-08-19 11:43:24 +02:00
feat: ThreadPool refac & dynamic thread management
This commit is contained in:
+7
-3
@@ -164,10 +164,14 @@ void Game::update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Game::updateBallsParallel(float dt) {
|
void Game::updateBallsParallel(float dt) {
|
||||||
const size_t chunkSize =
|
const size_t optimalThreads =
|
||||||
std::max(static_cast<size_t>(1),
|
ThreadUtils::calculateOptimalThreads(m_objects.size());
|
||||||
m_objects.size() / std::thread::hardware_concurrency());
|
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;
|
std::vector<std::future<void>> futures;
|
||||||
|
|
||||||
for (size_t i = 0; i < m_objects.size(); i += chunkSize) {
|
for (size_t i = 0; i < m_objects.size(); i += chunkSize) {
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
+14
-43
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
@@ -10,63 +11,33 @@
|
|||||||
class ThreadPool {
|
class ThreadPool {
|
||||||
public:
|
public:
|
||||||
explicit ThreadPool(size_t numThreads);
|
explicit ThreadPool(size_t numThreads);
|
||||||
|
|
||||||
~ThreadPool();
|
~ThreadPool();
|
||||||
|
|
||||||
[[nodiscard]] size_t getThreadCount() const { return workers.size(); }
|
[[nodiscard]] size_t getThreadCount() const { return workers.size(); }
|
||||||
|
|
||||||
|
void resize(size_t newSize);
|
||||||
|
|
||||||
template <class F, class... Args>
|
template <class F, class... Args>
|
||||||
auto enqueue(F &&f, Args &&...args)
|
auto enqueue(F&& f, Args&&... args)
|
||||||
-> std::future<std::invoke_result_t<F, Args...> >;
|
-> std::future<std::invoke_result_t<F, Args...>>;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void stopAllWorkers();
|
||||||
|
void startWorkers(size_t numWorkers);
|
||||||
|
|
||||||
std::vector<std::thread> workers;
|
std::vector<std::thread> workers;
|
||||||
|
std::queue<std::function<void()>> tasks;
|
||||||
std::queue<std::function<void()> > tasks;
|
|
||||||
|
|
||||||
std::mutex queueMutex;
|
std::mutex queueMutex;
|
||||||
std::condition_variable condition;
|
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>
|
template <class F, class... Args>
|
||||||
auto ThreadPool::enqueue(F &&f, Args &&...args)
|
auto ThreadPool::enqueue(F&& f, Args&&... args)
|
||||||
-> std::future<std::invoke_result_t<F, Args...> > {
|
-> std::future<std::invoke_result_t<F, Args...>> {
|
||||||
using return_type = 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::bind(std::forward<F>(f), std::forward<Args>(args)...));
|
||||||
|
|
||||||
std::future<return_type> res = task->get_future();
|
std::future<return_type> res = task->get_future();
|
||||||
@@ -79,4 +50,4 @@ auto ThreadPool::enqueue(F &&f, Args &&...args)
|
|||||||
|
|
||||||
condition.notify_one();
|
condition.notify_one();
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
namespace ThreadUtils {
|
namespace ThreadUtils {
|
||||||
@@ -5,4 +6,13 @@ inline size_t calculateSafeWorkerThreads(const size_t reserve = 2) {
|
|||||||
const size_t total = std::thread::hardware_concurrency();
|
const size_t total = std::thread::hardware_concurrency();
|
||||||
return total > reserve ? total - reserve : 1;
|
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
|
} // namespace ThreadUtils
|
||||||
|
|||||||
Reference in New Issue
Block a user