Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
thread_pool.h
Go to the documentation of this file.
1#ifndef BVH_V2_THREAD_POOL_H
2#define BVH_V2_THREAD_POOL_H
3
4#include <thread>
5#include <mutex>
6#include <condition_variable>
7#include <vector>
8#include <queue>
9#include <functional>
10
11namespace bvh::v2 {
12
14public:
15 using Task = std::function<void(size_t)>;
16
17 /// Creates a thread pool with the given number of threads (a value of 0 tries to autodetect
18 /// the number of threads and uses that as a thread count).
19 ThreadPool(size_t thread_count = 0) { start(thread_count); }
20
22 wait();
23 stop();
24 join();
25 }
26
27 inline void push(Task&& fun);
28 inline void wait();
29
30 size_t get_thread_count() const { return threads_.size(); }
31
32private:
33 static inline void worker(ThreadPool*, size_t);
34
35 inline void start(size_t);
36 inline void stop();
37 inline void join();
38
39 int busy_count_ = 0;
40 bool should_stop_ = false;
41 std::mutex mutex_;
42 std::vector<std::thread> threads_;
43 std::condition_variable avail_;
44 std::condition_variable done_;
45 std::queue<Task> tasks_;
46};
47
48void ThreadPool::push(Task&& task) {
49 {
50 std::unique_lock<std::mutex> lock(mutex_);
51 tasks_.emplace(std::move(task));
52 }
53 avail_.notify_one();
54}
55
57 std::unique_lock<std::mutex> lock(mutex_);
58 done_.wait(lock, [this] { return busy_count_ == 0 && tasks_.empty(); });
59}
60
61void ThreadPool::worker(ThreadPool* pool, size_t thread_id) {
62 while (true) {
63 Task task;
64 {
65 std::unique_lock<std::mutex> lock(pool->mutex_);
66 pool->avail_.wait(lock, [pool] { return pool->should_stop_ || !pool->tasks_.empty(); });
67 if (pool->should_stop_ && pool->tasks_.empty())
68 break;
69 task = std::move(pool->tasks_.front());
70 pool->tasks_.pop();
71 pool->busy_count_++;
72 }
73 task(thread_id);
74 {
75 std::unique_lock<std::mutex> lock(pool->mutex_);
76 pool->busy_count_--;
77 }
78 pool->done_.notify_one();
79 }
80}
81
82void ThreadPool::start(size_t thread_count) {
83 if (thread_count == 0)
84 thread_count = std::max(1u, std::thread::hardware_concurrency());
85 for (size_t i = 0; i < thread_count; ++i)
86 threads_.emplace_back(worker, this, i);
87}
88
90 {
91 std::unique_lock<std::mutex> lock(mutex_);
92 should_stop_ = true;
93 }
94 avail_.notify_all();
95}
96
98 for (auto& thread : threads_)
99 thread.join();
100}
101
102} // namespace bvh::v2
103
104#endif
std::vector< std::thread > threads_
Definition thread_pool.h:42
void push(Task &&fun)
Definition thread_pool.h:48
static void worker(ThreadPool *, size_t)
Definition thread_pool.h:61
size_t get_thread_count() const
Definition thread_pool.h:30
void start(size_t)
Definition thread_pool.h:82
std::queue< Task > tasks_
Definition thread_pool.h:45
std::condition_variable avail_
Definition thread_pool.h:43
std::condition_variable done_
Definition thread_pool.h:44
ThreadPool(size_t thread_count=0)
Creates a thread pool with the given number of threads (a value of 0 tries to autodetect the number o...
Definition thread_pool.h:19
std::function< void(size_t)> Task
Definition thread_pool.h:15
Definition bbox.h:9