diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2014-03-03 12:17:32 +0100 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2014-03-03 12:17:32 +0100 |
commit | b26f3d319bbb8400b5608ced69534260134915ea (patch) | |
tree | c6fe1e7c2f4063b31b69482ab8d43196c8098945 /include/llmr/util/threadpool.hpp | |
parent | e134e6a8f3c4fdfef81d1a4f4e74b04853ffa4db (diff) | |
download | qtlocation-mapboxgl-b26f3d319bbb8400b5608ced69534260134915ea.tar.gz |
add threadpool implementation
Diffstat (limited to 'include/llmr/util/threadpool.hpp')
-rw-r--r-- | include/llmr/util/threadpool.hpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/include/llmr/util/threadpool.hpp b/include/llmr/util/threadpool.hpp new file mode 100644 index 0000000000..736e65c527 --- /dev/null +++ b/include/llmr/util/threadpool.hpp @@ -0,0 +1,43 @@ +#ifndef LLMR_UTIL_THREADPOOL +#define LLMR_UTIL_THREADPOOL + +#include <pthread.h> +#include <forward_list> +#include <queue> + +namespace llmr { +namespace util { + +class Threadpool { + class Worker { + public: + Worker(Threadpool& pool); + ~Worker(); + static void *loop(void *ptr); + + private: + Threadpool& pool; + pthread_t thread; + }; + +public: + typedef void (*Callback)(void *); + Threadpool(Callback callback, int max_workers = 4); + + void add(void *data); + +private: + const int max_workers; + pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER; + pthread_cond_t condition = PTHREAD_COND_INITIALIZER; + std::forward_list<Worker> workers; + int worker_count = 0; + Callback callback; + std::queue<void *> tasks; +}; + +} +} + +#endif + |