diff options
Diffstat (limited to 'include/mbgl/util/threadpool.hpp')
-rw-r--r-- | include/mbgl/util/threadpool.hpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/include/mbgl/util/threadpool.hpp b/include/mbgl/util/threadpool.hpp new file mode 100644 index 0000000000..497d4e3083 --- /dev/null +++ b/include/mbgl/util/threadpool.hpp @@ -0,0 +1,45 @@ +#ifndef MBGL_UTIL_THREADPOOL +#define MBGL_UTIL_THREADPOOL + +#include <pthread.h> +#include <forward_list> +#include <queue> + +namespace mbgl { +namespace util { + +class Threadpool { +private: + class Worker { + public: + Worker(Threadpool& pool); + ~Worker(); + static void *loop(void *ptr); + + private: + Threadpool& pool; + pthread_t thread; + }; + +public: + 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_MUTEX_INITIALIZER; + pthread_cond_t condition = PTHREAD_COND_INITIALIZER; + std::forward_list<Worker> workers; + int worker_count = 0; + std::queue<Task> tasks; +}; + +extern std::unique_ptr<Threadpool> threadpool; + +} +} + +#endif + |