UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
ThreadPoolBase.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// eof: ThreadPoolBase.hxx
22//
23
24namespace Utils {
25
30
31 /*\
32 | _____ _ _ ___ _ ___
33 | |_ _| |_ _ _ ___ __ _ __| | _ \___ ___| | _ ) __ _ ___ ___
34 | | | | ' \| '_/ -_) _` / _` | _/ _ \/ _ \ | _ \/ _` (_-</ -_)
35 | |_| |_||_|_| \___\__,_\__,_|_| \___/\___/_|___/\__,_/__/\___|
36 \*/
37
39 protected:
40 // Interfaccia per Task
41 class Task {
42 public:
43 virtual ~Task() = default;
44 virtual void execute() = 0;
45 };
46 // Implementazione concreta di Task
47 template <typename Callable>
48 class ConcreteTask : public Task {
49 Callable callable;
50 public:
51 explicit ConcreteTask(Callable&& callable) : callable(std::move(callable)) {}
52 void execute() override { callable(); }
53 };
54
55 typedef std::function<void(void)> FUN;
56 //typedef std::unique_ptr<FUN> PFUN;
57
58 public:
59
60 //disable copy
61 ThreadPoolBase( ThreadPoolBase const & ) = delete;
65
66 ThreadPoolBase() = default;
67
68 //virtual void exec( std::function<void()> && ) = 0;
69 virtual void exec( FUN && ) = 0;
70
71 template <typename Func, typename... Args>
72 void
73 run( Func && func, Args && ... args ) {
74 this->exec(
75 std::bind(
76 std::forward<Func>(func),
77 std::forward<Args>(args)...
78 )
79 );
80 }
81
82 virtual void wait() = 0;
83 virtual unsigned thread_count() const = 0;
84 virtual char const * name() const = 0;
85 };
86
87 namespace tp {
88
89 /*\
90 | ___
91 | / _ \ _ _ ___ _ _ ___
92 | | | | | | | |/ _ \ | | |/ _ \
93 | | |_| | |_| | __/ |_| | __/
94 | \__\_\\__,_|\___|\__,_|\___|
95 \*/
96
97 class Queue {
98 public:
99
100 class TaskData {
101 std::function<void()> m_fun;
102 public:
103 TaskData( std::function<void()> && f ) : m_fun(std::move(f)) { }
104 TaskData( std::function<void()> & f ) : m_fun(f) { }
105 void operator()() { m_fun(); delete this; }
106 ~TaskData() = default;
107 };
108
109 private:
110
111 mutable std::mutex m_mutex;
112 std::vector<TaskData*> m_queue_data;
113
114 unsigned m_size, m_capacity;
115 unsigned m_push_ptr{0};
116 unsigned m_pop_ptr{0};
117
118 public:
119
120 Queue( Queue const & ) = delete;
121 Queue( Queue && ) = delete;
122 Queue& operator = ( Queue const & ) = delete;
123 Queue& operator = ( Queue && ) = delete;
124
125 explicit
126 Queue( unsigned capacity )
127 : m_queue_data( size_t( capacity+1 ) )
128 , m_size( capacity+1 )
129 , m_capacity( capacity )
130 { }
131
132 void
133 push( TaskData * task ) {
134 std::lock_guard<std::mutex> lock(m_mutex);
135 m_queue_data[m_push_ptr] = task;
136 if ( ++m_push_ptr == m_size ) m_push_ptr = 0;
137 }
138
139 TaskData *
140 pop() {
141 std::lock_guard<std::mutex> lock(m_mutex);
142 unsigned ipos{m_pop_ptr};
143 if ( ++m_pop_ptr == m_size ) m_pop_ptr = 0;
144 return m_queue_data[ipos];
145 }
146
147 unsigned
148 size() const {
149 std::lock_guard<std::mutex> lock(m_mutex);
150 return ((m_push_ptr + m_size) - m_pop_ptr) % m_size;
151 }
152
153 bool
154 empty() const {
155 std::lock_guard<std::mutex> lock(m_mutex);
156 return m_push_ptr == m_pop_ptr;
157 }
158
159 bool is_full() const {
160 std::lock_guard<std::mutex> lock(m_mutex);
161 unsigned sz = ((m_push_ptr + m_size) - m_pop_ptr) % m_size;
162 return sz >= m_capacity;
163 }
164
165 unsigned capacity() const { return m_capacity; }
166
168 void
170 std::lock_guard<std::mutex> lock(m_mutex);
171 while( m_push_ptr != m_pop_ptr ) delete pop();
172 }
173
174 void
175 resize( unsigned capacity ) {
176 std::lock_guard<std::mutex> lock(m_mutex);
177 while( m_push_ptr != m_pop_ptr ) delete pop();
178 m_size = capacity+1;
179 m_capacity = capacity;
180 m_queue_data.resize( m_size );
181 }
182
183 ~Queue() = default;
184 };
185 }
186
188
189}
190
191//
192// eof: ThreadPoolBase.hxx
193//
ConcreteTask(Callable &&callable)
Definition ThreadPoolBase.hxx:51
void execute() override
Definition ThreadPoolBase.hxx:52
Definition ThreadPoolBase.hxx:41
virtual ~Task()=default
virtual void execute()=0
void run(Func &&func, Args &&... args)
Definition ThreadPoolBase.hxx:73
ThreadPoolBase(ThreadPoolBase const &)=delete
virtual void wait()=0
virtual void exec(FUN &&)=0
std::function< void(void)> FUN
Definition ThreadPoolBase.hxx:55
virtual unsigned thread_count() const =0
virtual char const * name() const =0
ThreadPoolBase & operator=(ThreadPoolBase const &)=delete
ThreadPoolBase(ThreadPoolBase &&)=delete
Definition ThreadPoolBase.hxx:100
TaskData(std::function< void()> &&f)
Definition ThreadPoolBase.hxx:103
TaskData(std::function< void()> &f)
Definition ThreadPoolBase.hxx:104
void operator()()
Definition ThreadPoolBase.hxx:105
Queue(unsigned capacity)
Definition ThreadPoolBase.hxx:126
Queue(Queue &&)=delete
bool empty() const
Definition ThreadPoolBase.hxx:154
void resize(unsigned capacity)
Definition ThreadPoolBase.hxx:175
unsigned capacity() const
Definition ThreadPoolBase.hxx:165
Queue(Queue const &)=delete
void clear()
clear queue and delete tasks
Definition ThreadPoolBase.hxx:169
Queue & operator=(Queue const &)=delete
void push(TaskData *task)
Definition ThreadPoolBase.hxx:133
bool is_full() const
Definition ThreadPoolBase.hxx:159
unsigned size() const
Definition ThreadPoolBase.hxx:148
~Queue()=default
TaskData * pop()
Definition ThreadPoolBase.hxx:140
Definition ThreadPoolBase.hxx:87
Definition SystemUtils.cc:39