erebus
view liberebus/src/threadpool.h @ 47:4b91c9a501d8
minor fix of strange code structure in the thread func
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 01 Feb 2016 14:56:15 +0200 |
parents | c8a6fb04fefa |
children |
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 // passing num_threads == -1 auto-detects based on number of processors
36 explicit ThreadPool(int num_threads = -1);
37 ~ThreadPool();
39 void add_work(std::function<void ()> func);
40 void add_work(std::function<void ()> work_func, std::function<void ()> done_func);
41 void clear_work();
43 // returns the number of queued work items
44 int queued() const;
45 // returns the number of active threads
46 int active() const;
47 // returns number of pending work items (both in the queue and active)
48 int pending() const;
50 // waits for all work to be completed
51 long wait();
52 long wait(long timeout);
53 };
55 #endif // THREAD_POOL_H_