join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
thread.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CORE_THREAD_HPP
26#define JOIN_CORE_THREAD_HPP
27
28// C++.
29#include <system_error>
30#include <functional>
31#include <memory>
32#include <atomic>
33
34// C.
35#include <pthread.h>
36
37namespace join
38{
42 class Invoker
43 {
44 public:
48 Invoker () = delete;
49
50 private:
56 template <class Function, class... Args>
57 explicit Invoker (Function&& func, Args&&... args)
58 : Invoker (-1, 0, std::forward<Function> (func), std::forward<Args> (args)...)
59 {
60 }
61
69 template <class Function, class... Args>
70 explicit Invoker (int core, int prio, Function&& func, Args&&... args)
71 : _func (std::bind (std::forward<Function> (func), std::forward<Args> (args)...))
72 , _core (core)
73 , _priority (prio)
74 , _done (false)
75 {
76 pthread_create (&_handle, nullptr, _routine, this);
77 }
78
79 public:
84 Invoker (const Invoker& other) = delete;
85
91 Invoker& operator= (const Invoker& other) = delete;
92
97 Invoker (Invoker&& other) = delete;
98
104 Invoker& operator= (Invoker&& other) = delete;
105
109 ~Invoker () = default;
110
111 private:
117 static void* _routine (void* context);
118
123 void* routine ();
124
126 std::function<void ()> _func;
127
129 pthread_t _handle;
130
132 int _core = -1;
133
135 int _priority = 0;
136
138 std::atomic_bool _done;
139
141 friend class Thread;
142 };
143
147 class Thread
148 {
149 public:
153 Thread () noexcept = default;
154
160 template <class Function, class... Args>
161 explicit Thread (Function&& func, Args&&... args)
162 : Thread (-1, 0, std::forward<Function> (func), std::forward<Args> (args)...)
163 {
164 }
165
173 template <class Function, class... Args>
174 explicit Thread (int core, int prio, Function&& func, Args&&... args)
175 : _invoker (new Invoker (core, prio, std::forward<Function> (func), std::forward<Args> (args)...))
176 {
177 }
178
183 Thread (const Thread& other) = delete;
184
190 Thread& operator= (const Thread& other) = delete;
191
196 Thread (Thread&& other) noexcept;
197
203 Thread& operator= (Thread&& other) noexcept;
204
208 ~Thread ();
209
215 int affinity (int core);
216
223 static int affinity (pthread_t handle, int core);
224
229 int affinity () const noexcept;
230
236 int priority (int prio);
237
244 static int priority (pthread_t handle, int prio);
245
250 int priority () const noexcept;
251
256 bool joinable () const noexcept;
257
262 bool running () const noexcept;
263
267 void join () noexcept;
268
273 bool tryJoin () noexcept;
274
278 void cancel () noexcept;
279
284 void swap (Thread& other) noexcept;
285
290 pthread_t handle () const noexcept;
291
292 private:
294 std::unique_ptr<Invoker> _invoker;
295 };
296}
297
298#endif
thread invoker class.
Definition thread.hpp:43
Invoker()=delete
default constructor.
~Invoker()=default
destoyer.
Invoker(const Invoker &other)=delete
copy constructor.
Invoker & operator=(const Invoker &other)=delete
copy assignment.
Invoker(Invoker &&other)=delete
move constructor.
thread class.
Definition thread.hpp:148
int priority() const noexcept
get current thread priority.
Definition thread.cpp:224
int affinity() const noexcept
get current thread affinity.
Definition thread.cpp:159
~Thread()
destroys the thread object.
Definition thread.cpp:96
bool tryJoin() noexcept
performs a nonblocking join on the running thread.
Definition thread.cpp:264
Thread() noexcept=default
default constructor.
Thread(int core, int prio, Function &&func, Args &&... args)
creates a new thread object associated with a thread of execution.
Definition thread.hpp:174
Thread(const Thread &other)=delete
copy constructor.
void swap(Thread &other) noexcept
swap underlying handles of two thread objects.
Definition thread.cpp:291
void cancel() noexcept
cancel the running thread if any.
Definition thread.cpp:278
Thread & operator=(const Thread &other)=delete
copy assignment.
bool joinable() const noexcept
check if thread is joinable.
Definition thread.cpp:233
pthread_t handle() const noexcept
get the handle of the thread of execution. @retunr thread of execution handle.
Definition thread.cpp:300
bool running() const noexcept
check if the thread is running.
Definition thread.cpp:242
Definition acceptor.hpp:32
Definition error.hpp:137