paysages3d/src/core/ParallelPool.cpp

47 lines
964 B
C++

#include "ParallelPool.h"
#include "Logs.h"
#include "System.h"
#include "Thread.h"
static void *_threadFunction(void *data) {
ParallelPool *pool = (ParallelPool *)data;
pool->work();
return nullptr;
}
ParallelPool::ParallelPool() {
running = false;
}
ParallelPool::~ParallelPool() {
if (running) {
interrupt();
}
for (auto thread : threads) {
thread->join();
delete thread;
}
threads.clear();
}
void ParallelPool::start(int thread_count) {
if (running) {
Logs::error("System") << "Starting an already started parallel pool !" << endl;
return;
}
running = true;
if (thread_count < 0) {
thread_count = System::getCoreCount();
}
for (int i = 0; i < thread_count; i++) {
Thread *thread = new Thread(_threadFunction);
thread->start(this);
threads.push_back(thread);
}
}
void ParallelPool::interrupt() {
running = false;
}