Tacopie  3.0.0
Tacopie is a TCP Client & Server C++11 library.
thread_pool.hpp
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 <list>
29 #include <mutex>
30 #include <queue>
31 #include <thread>
32 #include <vector>
33 
34 namespace tacopie {
35 
36 namespace utils {
37 
41 class thread_pool {
42 public:
49  explicit thread_pool(std::size_t nb_threads);
50 
52  ~thread_pool(void);
53 
55  thread_pool(const thread_pool&) = delete;
57  thread_pool& operator=(const thread_pool&) = delete;
58 
59 public:
64  typedef std::function<void()> task_t;
65 
72  void add_task(const task_t& task);
73 
80  thread_pool& operator<<(const task_t& task);
81 
86  void stop(void);
87 
88 public:
92  bool is_running(void) const;
93 
94 public:
107  void set_nb_threads(std::size_t nb_threads);
108 
109 private:
113  void run(void);
114 
123  std::pair<bool, task_t> fetch_task_or_stop(void);
124 
128  bool should_stop(void) const;
129 
130 private:
134  std::list<std::thread> m_workers;
135 
139  std::atomic<std::size_t> m_max_nb_threads = ATOMIC_VAR_INIT(0);
140 
144  std::atomic<std::size_t> m_nb_running_threads = ATOMIC_VAR_INIT(0);
145 
149  std::atomic<bool> m_should_stop = ATOMIC_VAR_INIT(false);
150 
154  std::queue<task_t> m_tasks;
155 
159  std::mutex m_tasks_mtx;
160 
164  std::condition_variable m_tasks_condvar;
165 };
166 
167 } // namespace utils
168 
169 } // namespace tacopie
thread_pool(std::size_t nb_threads)
thread_pool & operator=(const thread_pool &)=delete
assignment operator
std::function< void()> task_t
Definition: thread_pool.hpp:64
Definition: io_service.hpp:48
void add_task(const task_t &task)
thread_pool & operator<<(const task_t &task)
void set_nb_threads(std::size_t nb_threads)
Definition: thread_pool.hpp:41
bool is_running(void) const