diff options
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 + |