move requests to global threadpool

This commit is contained in:
Konstantin Käfer
2014-03-03 12:55:32 +01:00
parent c076da0ae2
commit f850856cec
5 changed files with 25 additions and 26 deletions

View File

@@ -4,30 +4,33 @@
#include <pthread.h>
#include <forward_list>
#include <queue>
#include <string>
#include "std.hpp"
namespace llmr {
namespace util {
class Threadpool {
private:
class Worker {
public:
Worker(Threadpool& pool);
Worker(Threadpool& pool, const std::string name);
~Worker();
static void *loop(void *ptr);
private:
Threadpool& pool;
const std::string name;
pthread_t thread;
};
public:
typedef void (*Callback)(void *);
typedef std::pair<Callback, void *> Task;
Threadpool(int max_workers = 4);
typedef void (*Callback)(void *);
void add(Callback callback, void *data);
private:
typedef std::pair<Callback, void *> Task;
const int max_workers;
pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
pthread_cond_t condition = PTHREAD_COND_INITIALIZER;
@@ -36,6 +39,8 @@ private:
std::queue<Task> tasks;
};
extern std::unique_ptr<Threadpool> threadpool;
}
}