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 <functional>
30#include <memory>
31#include <atomic>
32
33// C.
34#include <pthread.h>
35
36namespace join
37{
41 class Invoker
42 {
43 public:
47 Invoker () = delete;
48
49 private:
55 template <class Function, class... Args>
56 explicit Invoker (Function&& func, Args&&... args)
57 : Invoker (-1, 0, std::forward<Function> (func), std::forward<Args> (args)...)
58 {
59 }
60
68 template <class Function, class... Args>
69 explicit Invoker (int core, int prio, Function&& func, Args&&... args)
70 : _func (std::bind (std::forward<Function> (func), std::forward<Args> (args)...))
71 , _core (core)
72 , _priority (prio)
73 , _done (false)
74 {
75 pthread_create (&_handle, nullptr, _routine, this);
76 }
77
78 public:
83 Invoker (const Invoker& other) = delete;
84
90 Invoker& operator= (const Invoker& other) = delete;
91
96 Invoker (Invoker&& other) = delete;
97
103 Invoker& operator= (Invoker&& other) = delete;
104
108 ~Invoker () = default;
109
110 private:
116 static void* _routine (void* context);
117
122 void* routine ();
123
125 std::function<void ()> _func;
126
128 pthread_t _handle;
129
131 int _core = -1;
132
134 int _priority = 0;
135
137 std::atomic_bool _done;
138
140 friend class Thread;
141 };
142
146 class Thread
147 {
148 public:
152 Thread () noexcept = default;
153
159 template <class Function, class... Args>
160 explicit Thread (Function&& func, Args&&... args)
161 : Thread (-1, 0, std::forward<Function> (func), std::forward<Args> (args)...)
162 {
163 }
164
172 template <class Function, class... Args>
173 explicit Thread (int core, int prio, Function&& func, Args&&... args)
174 : _invoker (new Invoker (core, prio, std::forward<Function> (func), std::forward<Args> (args)...))
175 {
176 }
177
182 Thread (const Thread& other) = delete;
183
189 Thread& operator= (const Thread& other) = delete;
190
195 Thread (Thread&& other) noexcept;
196
202 Thread& operator= (Thread&& other) noexcept;
203
207 ~Thread ();
208
214 int affinity (int core);
215
222 static int affinity (pthread_t handle, int core);
223
228 int affinity () const noexcept;
229
235 int priority (int prio);
236
243 static int priority (pthread_t handle, int prio);
244
249 int priority () const noexcept;
250
255 bool joinable () const noexcept;
256
261 bool running () const noexcept;
262
266 void join () noexcept;
267
272 bool tryJoin () noexcept;
273
277 void cancel () noexcept;
278
283 void swap (Thread& other) noexcept;
284
289 pthread_t handle () const noexcept;
290
291 private:
293 std::unique_ptr<Invoker> _invoker;
294 };
295}
296
297#endif
fixed-capacity move-only allocation-free alternative to std::function.
Definition function.hpp:44
thread invoker class.
Definition thread.hpp:42
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:147
int priority() const noexcept
get current thread priority.
Definition thread.cpp:225
int affinity() const noexcept
get current thread affinity.
Definition thread.cpp:160
~Thread()
destroys the thread object.
Definition thread.cpp:97
bool tryJoin() noexcept
performs a nonblocking join on the running thread.
Definition thread.cpp:265
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:173
Thread(const Thread &other)=delete
copy constructor.
void swap(Thread &other) noexcept
swap underlying handles of two thread objects.
Definition thread.cpp:292
void cancel() noexcept
cancel the running thread if any.
Definition thread.cpp:279
Thread & operator=(const Thread &other)=delete
copy assignment.
bool joinable() const noexcept
check if thread is joinable.
Definition thread.cpp:234
pthread_t handle() const noexcept
get the handle of the thread of execution. @retunr thread of execution handle.
Definition thread.cpp:301
bool running() const noexcept
check if the thread is running.
Definition thread.cpp:243
Definition acceptor.hpp:32
Definition error.hpp:144