adding thread pool

This commit is contained in:
2025-06-03 17:50:05 +02:00
parent 8dfb6af3ec
commit 66c42af1b8
5 changed files with 118 additions and 61 deletions

37
Application/ThreadPool.cpp git.filemode.normal_file
View File

@@ -0,0 +1,37 @@
#include <ThreadPool.h>
#include <ScopCore.h>
#include <thread>
ThreadPool::ThreadPool() : m_concurency(std::max(std::thread::hardware_concurrency(), DEFAULT_CONCURENCY)), m_waiting_count(m_concurency)
{
for(std::size_t i = 0; i < m_concurency; i++)
{
m_pool.emplace_back([this]
{
using namespace std::chrono_literals;
for(;;)
{
if(m_stop)
return;
if(!m_tasks.IsEmpty())
{
std::function<void()> task = std::move(m_tasks.Pop());
m_waiting_count--;
task();
m_waiting_count++;
}
else
std::this_thread::sleep_for(10ms);
}
});
}
Scop::Message("ThreadPool started with a capacity of %", m_concurency);
}
ThreadPool::~ThreadPool()
{
m_stop = true;
for(auto& thread : m_pool)
thread.join();
}