UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
ThreadPool1.hxx
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*\
2 | |
3 | Copyright (C) 2020 |
4 | |
5 | , __ , __ |
6 | /|/ \ /|/ \ |
7 | | __/ _ ,_ | __/ _ ,_ |
8 | | \|/ / | | | | \|/ / | | | |
9 | |(__/|__/ |_/ \_/|/|(__/|__/ |_/ \_/|/ |
10 | /| /| |
11 | \| \| |
12 | |
13 | Enrico Bertolazzi |
14 | Dipartimento di Ingegneria Industriale |
15 | Università degli Studi di Trento |
16 | email: enrico.bertolazzi@unitn.it |
17 | |
18\*--------------------------------------------------------------------------*/
19
20//
21// file: ThreadPool1.hxx
22//
23
24namespace Utils {
25
30
31 /*\
32 | _____ _ _ ____ _
33 | |_ _| |__ _ __ ___ __ _ __| | _ \ ___ ___ | |
34 | | | | '_ \| '__/ _ \/ _` |/ _` | |_) / _ \ / _ \| |
35 | | | | | | | | | __/ (_| | (_| | __/ (_) | (_) | |
36 | |_| |_| |_|_| \___|\__,_|\__,_|_| \___/ \___/|_|
37 \*/
38
45 class ThreadPool1 : public ThreadPoolBase {
46
47 using FUN = ThreadPoolBase::FUN;
48 //using PFUN = ThreadPoolBase::PFUN;
49
50 /*\
51 | __ __ _
52 | \ \ / /__ _ __| | _____ _ __
53 | \ \ /\ / / _ \| '__| |/ / _ \ '__|
54 | \ V V / (_) | | | < __/ |
55 | \_/\_/ \___/|_| |_|\_\___|_|
56 \*/
63 class Worker {
64
65 std::mutex m_task_mutex;
66 std::condition_variable m_task_cv;
67
68 bool m_active{true};
69 std::thread m_running_thread;
70 bool m_task_done{true};
71 FUN m_task;
72
74 void
75 worker_loop() {
76 std::unique_lock<std::mutex> lock(m_task_mutex);
77 while ( m_active ) {
78 while ( m_task_done ) m_task_cv.wait( lock );
79 m_task();
80 m_task_done = true;
81 m_task_cv.notify_one();
82 }
83 }
84
85 public:
86
87 //disable copy
88 Worker( Worker const & ) = delete;
89 //Worker( Worker && ) = delete;
90 Worker& operator = ( Worker const & ) = delete;
91 Worker& operator = ( Worker && ) = delete;
92
96 Worker() : m_running_thread( std::thread( &Worker::worker_loop, this ) ) {}
97
101 ~Worker() {
102 auto dummy_task = []()->void{};
103 m_active = false; // deactivate computation
104 exec( dummy_task );
105 if ( m_running_thread.joinable() ) m_running_thread.join(); // wait thread for exiting
106 }
107
111 Worker( Worker && rhs ) noexcept
112 : m_active(rhs.m_active)
113 , m_running_thread(std::move(rhs.m_running_thread))
114 , m_task(std::move(rhs.m_task))
115 {}
116
120 void
121 wait() {
122 std::unique_lock<std::mutex> lock(m_task_mutex);
123 while ( !m_task_done ) m_task_cv.wait( lock );
124 }
125
131 void
132 exec( FUN && fun ) {
133 // aspetto che pointer si liberi se occupato
134 std::unique_lock<std::mutex> lock(m_task_mutex);
135 while ( !m_task_done ) m_task_cv.wait( lock );
136 m_task_done = false;
137 m_task = std::move(fun); // cambia funzione da eseguire
138 m_task_cv.notify_one();
139 }
140 };
141
143 std::size_t m_thread_to_send{0};
144
146 std::vector<Worker> m_workers;
147
148 public:
149
155 explicit
157 unsigned nthread = std::max(
158 unsigned(1),
159 unsigned(std::thread::hardware_concurrency()-1)
160 )
161 ) : ThreadPoolBase(), m_workers( size_t( nthread ) ) { }
162
166 virtual
167 ~ThreadPool1() { m_workers.clear(); }
168
174 void
175 exec( FUN && fun ) override {
176 m_workers[m_thread_to_send].exec( std::move(fun) );
177 if ( ++m_thread_to_send >= m_workers.size() ) m_thread_to_send = 0;
178 }
179
181 void wait() override { for ( auto && w : m_workers ) w.wait(); }
182
188 unsigned thread_count() const override { return unsigned(m_workers.size()); }
189
190 static char const * Name() { return "ThreadPool1"; }
191
192 char const * name() const override { return Name(); }
193
194 };
195
197
198}
199
200//
201// eof: ThreadPool1.hxx
202//
static char const * Name()
Returns the name of the thread pool.
Definition ThreadPool1.hxx:190
void wait() override
Waits for all tasks to finish.
Definition ThreadPool1.hxx:181
virtual ~ThreadPool1()
Destroys the ThreadPool1 and stops all worker threads.
Definition ThreadPool1.hxx:167
ThreadPool1(unsigned nthread=std::max(unsigned(1), unsigned(std::thread::hardware_concurrency() -1)))
Constructs a new ThreadPool1 with a specified number of threads.
Definition ThreadPool1.hxx:156
unsigned thread_count() const override
Returns the number of threads in the pool.
Definition ThreadPool1.hxx:188
char const * name() const override
Definition ThreadPool1.hxx:192
void exec(FUN &&fun) override
Executes a task in the thread pool.
Definition ThreadPool1.hxx:175
ThreadPoolBase(ThreadPoolBase const &)=delete
std::function< void(void)> FUN
Definition ThreadPoolBase.hxx:55
Definition SystemUtils.cc:39