erebus

view liberebus/src/threadpool.h @ 31:53a98c148bf8

- introduced SurfaceGeometry to carry all the geometric information input to BRDF sampling and evaluation functions. - made Reflectance keep an (optional) pointer to its material - simplified PhongRefl::sample_dir, with the help of SurfaceGeometry - worked around microsoft's broken std::thread implementation's deadlock on join
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 07 Jun 2014 09:14:17 +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_