Tacopie  3.0.0
Tacopie is a TCP Client & Server C++11 library.
thread_pool.hpp
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2016-2017 Simon Ninon <simon.ninon@gmail.com>
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #pragma once
24 
25 #include <atomic>
26 #include <condition_variable>
27 #include <functional>
28 #include <mutex>
29 #include <queue>
30 #include <thread>
31 #include <vector>
32 
33 namespace tacopie {
34 
35 namespace utils {
36 
37 class thread_pool {
38 public:
45  explicit thread_pool(std::size_t nb_threads);
46 
48  ~thread_pool(void);
49 
51  thread_pool(const thread_pool&) = delete;
53  thread_pool& operator=(const thread_pool&) = delete;
54 
55 public:
60  typedef std::function<void()> task_t;
61 
68  void add_task(const task_t& task);
69 
76  thread_pool& operator<<(const task_t& task);
77 
82  void stop(void);
83 
84 public:
88  bool is_running(void) const;
89 
90 public:
103  void set_nb_threads(std::size_t nb_threads);
104 
105 private:
109  void run(void);
110 
116  task_t fetch_task(void);
117 
121  bool should_stop(void) const;
122 
123 private:
127  std::vector<std::thread> m_workers;
128 
132  std::size_t m_nb_threads;
133 
137  std::atomic<bool> m_should_stop = ATOMIC_VAR_INIT(false);
138 
142  std::queue<task_t> m_tasks;
143 
147  std::mutex m_tasks_mtx;
148 
152  std::condition_variable m_tasks_condvar;
153 };
154 
155 } // namespace utils
156 
157 } // namespace tacopie
std::condition_variable m_tasks_condvar
Definition: thread_pool.hpp:152
thread_pool(std::size_t nb_threads)
std::size_t m_nb_threads
Definition: thread_pool.hpp:132
std::mutex m_tasks_mtx
Definition: thread_pool.hpp:147
thread_pool & operator=(const thread_pool &)=delete
assignment operator
std::function< void()> task_t
Definition: thread_pool.hpp:60
std::atomic< bool > m_should_stop
Definition: thread_pool.hpp:137
bool should_stop(void) const
Definition: io_service.hpp:48
void add_task(const task_t &task)
std::vector< std::thread > m_workers
Definition: thread_pool.hpp:127
thread_pool & operator<<(const task_t &task)
void set_nb_threads(std::size_t nb_threads)
Definition: thread_pool.hpp:37
std::queue< task_t > m_tasks
Definition: thread_pool.hpp:142
bool is_running(void) const