IAtari
Genetic algorithm generating AI capable to play Atari2600 games.
ThreadPool.h
Go to the documentation of this file.
1 #ifndef THREADPOOL_H_INCLUDED
2 #define THREADPOOL_H_INCLUDED
3 /*
4  Extended from code:
5  Copyright (c) 2012 Jakob Progsch, Václav Zeman
6 
7  This software is provided 'as-is', without any express or implied
8  warranty. In no event will the authors be held liable for any damages
9  arising from the use of this software.
10 
11  Permission is granted to anyone to use this software for any purpose,
12  including commercial applications, and to alter it and redistribute it
13  freely, subject to the following restrictions:
14 
15  1. The origin of this software must not be misrepresented; you must not
16  claim that you wrote the original software. If you use this software
17  in a product, an acknowledgment in the product documentation would be
18  appreciated but is not required.
19 
20  2. Altered source versions must be plainly marked as such, and must not be
21  misrepresented as being the original software.
22 
23  3. This notice may not be removed or altered from any source
24  distribution.
25 */
26 
27 #include <cstddef>
28 #include <vector>
29 #include <thread>
30 #include <queue>
31 #include <mutex>
32 #include <condition_variable>
33 #include <memory>
34 #include <future>
35 #include <functional>
36 
37 namespace Utils {
38 
39 class ThreadPool {
40 public:
41  ThreadPool() = default;
42  ~ThreadPool();
43 
44  // create worker threads. This version has no initializers.
45  void initialize(std::size_t);
46 
47  // add an extra thread. The thread calls initializer() before doing anything,
48  // so that the user can initialize per-thread data structures before doing work.
49  void add_thread(std::function<void()> initializer);
50  template<class F, class... Args>
51  auto add_task(F&& f, Args&&... args)
52  -> std::future<typename std::result_of<F(Args...)>::type>;
53 private:
54  std::vector<std::thread> m_threads;
55  std::queue<std::function<void()>> m_tasks;
56 
57  std::mutex m_mutex;
58  std::condition_variable m_condvar;
59  bool m_exit{false};
60 };
61 
62 inline void ThreadPool::add_thread(std::function<void()> initializer) {
63  m_threads.emplace_back([this, initializer] {
64  initializer();
65  for (;;) {
66  std::function<void()> task;
67  {
68  std::unique_lock<std::mutex> lock(m_mutex);
69  m_condvar.wait(lock, [this]{ return m_exit || !m_tasks.empty(); });
70  if (m_exit && m_tasks.empty()) {
71  return;
72  }
73  task = std::move(m_tasks.front());
74  m_tasks.pop();
75  }
76  task();
77  }
78  });
79 }
80 
81 inline void ThreadPool::initialize(size_t threads) {
82  for (size_t i = 0; i < threads; i++) {
83  add_thread( [](){} /* null function */);
84  }
85 }
86 
87 template<class F, class... Args>
88 auto ThreadPool::add_task(F&& f, Args&&... args)
89  -> std::future<typename std::result_of<F(Args...)>::type> {
90  using return_type = typename std::result_of<F(Args...)>::type;
91 
92  auto task = std::make_shared< std::packaged_task<return_type()> >(
93  std::bind(std::forward<F>(f), std::forward<Args>(args)...)
94  );
95 
96  std::future<return_type> res = task->get_future();
97  {
98  std::unique_lock<std::mutex> lock(m_mutex);
99  m_tasks.emplace([task](){(*task)();});
100  }
101  m_condvar.notify_one();
102  return res;
103 }
104 
106  {
107  std::unique_lock<std::mutex> lock(m_mutex);
108  m_exit = true;
109  }
110  m_condvar.notify_all();
111  for (std::thread & worker: m_threads) {
112  worker.join();
113  }
114 }
115 
116 class ThreadGroup {
117 public:
118  ThreadGroup(ThreadPool & pool) : m_pool(pool) {}
119  template<class F, class... Args>
120  void add_task(F&& f, Args&&... args) {
121  m_taskresults.emplace_back(
122  m_pool.add_task(std::forward<F>(f), std::forward<Args>(args)...)
123  );
124  }
125  void wait_all() {
126  for (auto && result: m_taskresults) {
127  result.get();
128  }
129  }
130 private:
131  ThreadPool & m_pool;
132  std::vector<std::future<void>> m_taskresults;
133 };
134 
135 }
136 
137 #endif
void add_thread(std::function< void()> initializer)
Definition: ThreadPool.h:62
ThreadGroup(ThreadPool &pool)
Definition: ThreadPool.h:118
Definition: ThreadPool.h:116
Definition: ThreadPool.h:39
ThreadPool()=default
void add_task(F &&f, Args &&... args)
Definition: ThreadPool.h:120
void wait_all()
Definition: ThreadPool.h:125
auto add_task(F &&f, Args &&... args) -> std::future< typename std::result_of< F(Args...)>::type >
Definition: ThreadPool.h:88
~ThreadPool()
Definition: ThreadPool.h:105
A set of useful method and class.
Definition: Random.cpp:27
void initialize(std::size_t)
Definition: ThreadPool.h:81