erebus

view liberebus/src/threadpool.h @ 26:c8a6fb04fefa

multithreadededit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Jun 2014 19:19:40 +0300
parents 4336acf8389d
children d15ee526daa6
line source
1 #ifndef THREAD_POOL_H_
2 #define THREAD_POOL_H_
4 #include <list>
5 #include <functional>
6 #include <thread>
7 #include <mutex>
8 #include <condition_variable>
10 class ThreadPool {
11 private:
12 int num_threads;
13 std::thread *thread; // array of threads
15 struct WorkItem {
16 std::function<void ()> work;
17 std::function<void ()> done;
18 };
20 int qsize;
21 std::list<WorkItem> workq;
22 mutable std::mutex workq_mutex;
23 std::condition_variable workq_condvar;
25 int nactive; // number of active workers (not sleeping)
27 // condvar used by wait
28 std::condition_variable done_condvar;
30 bool quit;
32 void thread_func();
34 public:
35 ThreadPool(int num_threads = -1);
36 ~ThreadPool();
38 void add_work(std::function<void ()> func);
39 void add_work(std::function<void ()> work_func, std::function<void ()> done_func);
41 // returns the number of queued work items
42 int queued() const;
43 // returns the number of active threads
44 int active() const;
45 // returns number of pending work items (both in the queue and active)
46 int pending() const;
48 // waits for all work to be completed
49 long wait();
50 long wait(long timeout);
51 };
53 #endif // THREAD_POOL_H_