FRI-OS
thread.hpp
Go to the documentation of this file.
1 
4 #ifndef __FRIOS_POSIX_THREADS_THREAD_HPP__
5 #define __FRIOS_POSIX_THREADS_THREAD_HPP__
6 
7 #include <frios/posix/error.hpp>
9 
10 #include <cassert>
11 
12 #include <pthread.h>
13 
14 namespace frios {
15 namespace posix {
16 
18 
25 class thread
26 {
27 private:
28  unique<pthread_t> _thread;
29  bool _joined;
30  bool _detached;
31 
32  void _start(
33  const pthread_attr_t* thread_attr,
34  void* thread_main(void*),
35  void* thread_data
36  )
37  {
38  int fail = pthread_create(
39  &_thread,
40  thread_attr,
41  thread_main,
42  thread_data
43  );
44  if(fail) throw create_error(fail);
45  }
46 
47  thread(const thread&) = delete;
48 public:
50 
54  {
55  create_error(int error_code)
56  : error(error_code)
57  { }
58  };
59 
61 
67  void* thread_main(void*),
68  void* thread_data
69  ): _joined(false)
70  , _detached(false)
71  {
72  _start(0, thread_main, thread_data);
73  }
74 
76 
80  const thread_attr& attr,
81  void* thread_main(void*),
82  void* thread_data
83  ): _joined(false)
84  , _detached(attr.get_detach_state() == thread_attr::detach_state::detached)
85  {
86  _start(&attr._attr, thread_main, thread_data);
87  }
88 
90  thread(thread&& tmp)
91  : _thread(std::move(tmp._thread))
92  , _joined(tmp._joined)
93  , _detached(tmp._detached)
94  { }
95 
97 
102  ~thread(void)
103  {
104  if(_thread.valid())
105  assert(joined() || detached());
106  }
107 
109 
115  bool joinable(void) const
116  {
117  return !detached() && !joined();
118  }
119 
121 
127  bool detached(void) const
128  {
129  return _detached;
130  }
131 
133 
139  bool joined(void) const
140  {
141  return _joined;
142  }
143 
145 
156  void detach(void)
157  {
158  assert(!joined() && !detached());
159  int fail = pthread_detach(_thread);
160  if(fail) throw error(fail);
161  _detached = true;
162  }
163 
165 
177  void* join(void)
178  {
179  assert(!joined() && !detached());
180  void* result = 0;
181  int fail = pthread_join(_thread, &result);
182  if(fail) throw error(fail);
183  _joined = true;
184  return result;
185  }
186 
188 
195  void wait_for(void)
196  {
197  assert(!detached());
198  if(!joined()) join();
199  }
200 };
201 
202 } // namespace posix
203 } // namespace frios
204 
205 #endif // include guard
void * join(void)
Joins the thread and retrieves the return value.
Definition: thread.hpp:177
Wrapper around POSIX thread attributes.
Definition: thread_attr.hpp:22
void wait_for(void)
Waits for the thread to finish.
Definition: thread.hpp:195
bool detached(void) const
Indicates that the thread has been detached.
Definition: thread.hpp:127
Base class for all POSIX exceptions.
Definition: error.hpp:14
bool joinable(void) const
Indicates that the thread is running and it is not detached nor joined.
Definition: thread.hpp:115
thread(void *thread_main(void *), void *thread_data)
Starts a new thread with the specified main function and data.
Definition: thread.hpp:66
Wrapper around POSIX thread attributes.
bool joined(void) const
Indicates that the thread has been joined.
Definition: thread.hpp:139
thread(const thread_attr &attr, void *thread_main(void *), void *thread_data)
Starts a new thread with the specified attributes, main function and data.
Definition: thread.hpp:79
void detach(void)
Detaches the thread.
Definition: thread.hpp:156
thread(thread &&tmp)
Threads are movable.
Definition: thread.hpp:90
Declaration of exception classes for POSIX errors.
error(int error_code)
Construction from POSIX error code.
Definition: error.hpp:26
~thread(void)
Destructor releases any resources associated with the running thread.
Definition: thread.hpp:102
Wrapper around a POSIX thread.
Definition: thread.hpp:25
Exception class for thread creation errors.
Definition: thread.hpp:53