VulcanoLE/src/VUtils/Pool.cpp

40 lines
935 B
C++

#include <VUtils/Pool.h>
#include <VUtils/Logging.h>
namespace VUtils {
Pool::Pool(const char *name) : m_name(name) {
}
Pool::~Pool() {
delete[] m_threads;
}
void Pool::setThreadCount(int count) {
if (m_isCreated) return;
m_count = count == -1 ? std::thread::hardware_concurrency() : count;
}
void Pool::joinFirstThread() {
if (!m_isCreated) return;
if (m_threads[0].joinable()) {
m_threads[0].join();
}
}
void Pool::create(PoolWorker &worker) {
if (m_isCreated) return;
m_isCreated = true;
m_worker = &worker;
m_threads = new std::thread[m_count];
for (int i = 0; i < m_count; ++i) {
m_threads[i] = std::thread(&Pool::execute, this);
}
DBG("Created %d Threads for ThreadPool %s", m_count, m_name)
}
void Pool::execute() {
m_worker->run();
}
}