feat(ThreadPool): Added proper thread pool separate from main game thread

This commit is contained in:
Yimura 2021-09-18 22:08:46 +02:00
parent 1802c73846
commit f58f433097
2 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,89 @@
#include "thread_pool.hpp"
namespace big
{
thread_pool::thread_pool() : m_accept_jobs(true), m_data_condition(), m_job_stack(), m_lock()
{
this->m_managing_thread = std::thread(&thread_pool::create, this);
g_thread_pool = this;
}
thread_pool::~thread_pool()
{
g_thread_pool = nullptr;
}
void thread_pool::create()
{
int thread_count = std::thread::hardware_concurrency();
LOG(INFO) << "Allocating " << thread_count << " threads in thread pool.";
this->m_thread_pool.reserve(thread_count);
for (int i = 0; i < thread_count; i++)
this->m_thread_pool.push_back(std::thread(&thread_pool::run, this));
}
void thread_pool::destroy()
{
this->m_managing_thread.join();
this->done();
for (int i = 0; i < this->m_thread_pool.size(); i++)
this->m_thread_pool.at(i).join();
}
void thread_pool::done()
{
std::unique_lock<std::mutex> lock(this->m_lock);
this->m_accept_jobs = false;
lock.unlock();
this->m_data_condition.notify_all();
}
void thread_pool::push(std::function<void()> func)
{
if (func)
{
std::unique_lock<std::mutex> lock(this->m_lock);
this->m_job_stack.push(std::move(func));
lock.unlock();
this->m_data_condition.notify_all();
}
}
void thread_pool::run()
{
for (;;)
{
std::unique_lock<std::mutex> lock(this->m_lock);
this->m_data_condition.wait(lock, [this]()
{
return !this->m_job_stack.empty() || !this->m_accept_jobs;
});
if (!this->m_accept_jobs) return;
if (this->m_job_stack.empty()) continue;
auto job = std::move(this->m_job_stack.top());
this->m_job_stack.pop();
lock.unlock();
try
{
std::invoke(std::move(job));
}
catch (const std::exception& e)
{
LOG(WARNING) << "Exception thrown while executing job in thread:" << std::endl << e.what();
}
}
LOG(INFO) << "Thread " << std::this_thread::get_id() << " exiting...";
}
}

View File

@ -0,0 +1,28 @@
#pragma once
namespace big
{
class thread_pool
{
std::atomic<bool> m_accept_jobs;
std::condition_variable m_data_condition;
std::stack<std::function<void()>> m_job_stack;
std::mutex m_lock;
std::vector<std::thread> m_thread_pool;
std::thread m_managing_thread;
public:
thread_pool();
~thread_pool();
void destroy();
void push(std::function<void()> func);
private:
void create();
void done();
void run();
};
inline thread_pool* g_thread_pool{};
}