ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
thread_pool.hpp
Go to the documentation of this file.
1 #ifndef H_THREAD_POOL
2 #define H_THREAD_POOL
3 
4 #include <algorithm>
5 #include <thread>
6 #include <future>
7 #include <memory>
8 #include <functional>
9 #include <queue>
10 #include <mutex>
11 #include <vector>
12 #include <atomic>
13 #include <condition_variable>
14 
15 class ThreadPool{
16 public:
17  ThreadPool();
18  explicit ThreadPool(std::size_t num_threads);
19  ~ThreadPool();
20 
21  std::size_t Size() const;
22  void Resize(size_t num_threads);
23 
24  template<typename FuncType, typename...ArgTypes>
25  auto Push(FuncType &&func, ArgTypes&&... args) -> std::future<decltype(func(args...))>;
26 
27 private:
28  ThreadPool(const ThreadPool &) = delete;
29  ThreadPool& operator=(const ThreadPool &) = delete;
30  ThreadPool(ThreadPool &&) = delete;
31  ThreadPool& operator=(ThreadPool &&) = delete;
32 
33  void DoTasksFromQueue(size_t ithread);
34  bool ReadyToAct(size_t ithread, std::unique_ptr<std::function<void()> > &task);
35 
36  class Queue{
37  public:
38  using FuncPtr = std::unique_ptr<std::function<void()> >;
39 
40  Queue() = default;
41  ~Queue() = default;
42 
43  void Push(FuncPtr &func);
44  FuncPtr Pop();
45 
46  private:
47  Queue(const Queue &) = delete;
48  Queue& operator=(const Queue &) = delete;
49  Queue(Queue &&) = delete;
50  Queue& operator=(Queue &&) = delete;
51 
52  std::queue<FuncPtr> queue_;
53  std::mutex mutex_;
54  };
55 
57  std::vector<std::unique_ptr<std::thread> > threads_;
58  std::vector<std::shared_ptr<std::atomic<bool> > > stop_thread_now_;
59  std::atomic<bool> stop_at_empty_;
60 
61  std::mutex mutex_;
62  std::condition_variable cv_;
63 };
64 
65 template<typename FuncType, typename...ArgTypes>
66 auto ThreadPool::Push(FuncType &&func, ArgTypes&&... args) -> std::future<decltype(func(args...))>{
67  auto task = std::make_shared<std::packaged_task<decltype(func(args...))()> >(std::bind(std::forward<FuncType>(func), std::forward<ArgTypes>(args)...));
68  std::unique_ptr<std::function<void()> > pkg_func(new std::function<void()>([task](){(*task)();}));
69  tasks_.Push(pkg_func);
70  std::lock_guard<std::mutex> lock(mutex_);
71  cv_.notify_one();
72  return task->get_future();
73 }
74 
75 #endif
std::atomic< bool > stop_at_empty_
Definition: thread_pool.hpp:59
ThreadPool & operator=(const ThreadPool &)=delete
std::size_t Size() const
Definition: thread_pool.cpp:49
Queue tasks_
Definition: thread_pool.hpp:56
void DoTasksFromQueue(size_t ithread)
Definition: thread_pool.cpp:78
std::mutex mutex_
Definition: thread_pool.hpp:61
tuple args
Definition: compile.py:193
std::queue< FuncPtr > queue_
Definition: thread_pool.hpp:52
std::vector< std::shared_ptr< std::atomic< bool > > > stop_thread_now_
Definition: thread_pool.hpp:58
auto Push(FuncType &&func, ArgTypes &&...args) -> std::future< decltype(func(args...))>
Definition: thread_pool.hpp:66
std::mutex mutex_
Definition: thread_pool.hpp:53
Queue & operator=(const Queue &)=delete
bool ReadyToAct(size_t ithread, std::unique_ptr< std::function< void()> > &task)
Definition: thread_pool.cpp:93
std::vector< std::unique_ptr< std::thread > > threads_
Definition: thread_pool.hpp:57
void Resize(size_t num_threads)
Definition: thread_pool.cpp:53
std::unique_ptr< std::function< void()> > FuncPtr
Definition: thread_pool.hpp:38
void Push(FuncPtr &func)
std::condition_variable cv_
Definition: thread_pool.hpp:62