join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
thread.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_THREAD_HPP__
26#define __JOIN_THREAD_HPP__
27
28// C++.
29#include <system_error>
30#include <functional>
31#include <memory>
32#include <thread>
33#include <atomic>
34
35// C.
36#include <pthread.h>
37
38namespace join
39{
43 class Invoker
44 {
45 public:
49 Invoker () = delete;
50
51 private:
57 template <class Function, class... Args>
58 explicit Invoker (Function&& func, Args&&... args)
59 : _func (std::bind (std::forward <Function> (func), std::forward <Args> (args)...)),
60 _done (false)
61 {
62 pthread_attr_init (&_attr);
63 pthread_create (&_handle, &_attr, _routine, this);
64 }
65
66 public:
71 Invoker (const Invoker& other) = delete;
72
78 Invoker& operator= (const Invoker& other) = delete;
79
84 Invoker (Invoker&& other) = delete;
85
91 Invoker& operator= (Invoker&& other) = delete;
92
96 ~Invoker ();
97
98 private:
103 pthread_t handle ();
104
110 static void * _routine (void * context);
111
116 void * routine (void);
117
119 std::function <void ()> _func;
120
122 pthread_attr_t _attr;
123
125 pthread_t _handle;
126
128 std::atomic_bool _done;
129
131 friend class Thread;
132 };
133
137 class Thread
138 {
139 public:
143 Thread () noexcept = default;
144
150 template <class Function, class... Args>
151 explicit Thread (Function&& func, Args&&... args)
152 : _invoker (new Invoker (std::forward <Function> (func), std::forward <Args> (args)...))
153 {
154 }
155
160 Thread (const Thread& other) = delete;
161
167 Thread& operator= (const Thread& other) = delete;
168
173 Thread (Thread&& other) noexcept;
174
180 Thread& operator= (Thread&& other) noexcept;
181
185 ~Thread ();
186
191 bool joinable () const noexcept;
192
197 bool running () const noexcept;
198
202 void join ();
203
208 bool tryJoin ();
209
213 void cancel ();
214
219 void swap (Thread& other);
220
221 private:
223 std::unique_ptr <Invoker> _invoker;
224 };
225}
226
227#endif
thread invoker class.
Definition thread.hpp:44
Invoker()=delete
default constructor.
Invoker(const Invoker &other)=delete
copy constructor.
Invoker & operator=(const Invoker &other)=delete
copy assignment.
Invoker(Invoker &&other)=delete
move constructor.
~Invoker()
destoyer.
Definition thread.cpp:38
thread class.
Definition thread.hpp:138
~Thread()
destroys the thread object.
Definition thread.cpp:96
bool tryJoin()
performs a nonblocking join on the running thread.
Definition thread.cpp:136
Thread() noexcept=default
default constructor.
Thread(const Thread &other)=delete
copy constructor.
void swap(Thread &other)
swap underlying handles of two thread objects.
Definition thread.cpp:163
Thread & operator=(const Thread &other)=delete
copy assignment.
void cancel()
cancel the running thread if any.
Definition thread.cpp:150
bool joinable() const noexcept
check if thread is joinable.
Definition thread.cpp:105
bool running() const noexcept
check if the thread is running.
Definition thread.cpp:114
Definition acceptor.hpp:32
Definition error.hpp:106