add threadpool implementation

This commit is contained in:
Konstantin Käfer
2014-03-03 12:17:32 +01:00
parent e134e6a8f3
commit b26f3d319b
6 changed files with 164 additions and 117 deletions

51
src/util/threadpool.cpp Normal file
View File

@@ -0,0 +1,51 @@
#include <llmr/util/threadpool.hpp>
using namespace llmr::util;
Threadpool::Threadpool(Callback callback, int max_workers)
: max_workers(max_workers),
callback(callback) {
}
void Threadpool::add(void *data) {
if (worker_count < max_workers) {
workers.emplace_front(*this);
worker_count++;
}
pthread_mutex_lock(&mutex);
tasks.push(data);
pthread_mutex_unlock(&mutex);
pthread_cond_signal(&condition);
}
Threadpool::Worker::Worker(Threadpool& pool)
: pool(pool) {
pthread_create(&thread, nullptr, loop, (void *)this);
}
Threadpool::Worker::~Worker() {
pthread_cancel(thread);
}
void *Threadpool::Worker::loop(void *ptr) {
Worker *worker = static_cast<Worker *>(ptr);
Threadpool& pool = worker->pool;
pthread_setname_np("worker");
pthread_mutex_lock(&pool.mutex);
while (true) {
if (pool.tasks.size()) {
void *task = pool.tasks.front();
pool.tasks.pop();
pthread_mutex_unlock(&pool.mutex);
pool.callback(task);
pthread_mutex_lock(&pool.mutex);
} else {
pthread_cond_wait(&pool.condition, &pool.mutex);
}
}
return nullptr;
}