cpp-pthread  (v1.7.3)
Simple C++ wrapper to pthread functions.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
thread.hpp
1 //
2 // thread.hpp
3 // cpp-pthread
4 //
5 // Created by herbert koelman on 18/03/2016.
6 // Copyright © 2016 urbix-software. All rights reserved.
7 //
8 
9 #ifndef PTHREAD_THREAD_HPP
10 #define PTHREAD_THREAD_HPP
11 
12 // must be include as first hearder file of each source code file (see IBM's
13 // recommandation for more info p.285 §8.3.1).
14 #include <pthread.h>
15 
16 #include <iostream>
17 #include <string>
18 #include <functional>
19 #include <memory> // std::auto_ptr, std::unique_ptr
20 #include <list>
21 #include <cstddef>
22 
23 #include "pthread/config.h"
24 #include "pthread/exceptions.hpp"
25 #include "pthread/mutex.hpp"
26 #include "pthread/lock_guard.hpp"
27 
28 
29 namespace pthread {
30 
43  void *thread_startup_runnable (void *);
44 
47  enum class thread_status{
48  not_a_thread,
49  a_thread
50  };
51 
58  class runnable {
59  private:
60 
61  public:
65 #if __cplusplus < 201103L
66  virtual void run () throw() = 0 ;
67 #else
68  virtual void run () noexcept = 0 ;
69 #endif
70 
71  virtual ~runnable() {
72  // Intentionally unimplemented...
73  };
74  };
75 
91  class thread {
92  public:
93 
97  thread();
98 
106  thread( const runnable &runner, const std::size_t stack_size = 0 );
107 
114  thread( thread&& other); // NOSONAR this is std interface and cannot be changed
115 
118  thread(const thread &) = delete ;
119 
123  virtual ~thread();
124 
135  void join();
136 
139  bool joinable() const { return _thread != 0 ;};
140 
152  int cancel();
153 
156  inline thread_status status() { return _status ;};
157 
160  thread& operator=(const thread&) = delete ;
161 
167  thread& operator=(thread&& other); // NOSONAR this is a std method signature
168 
169  private:
170 
175  void swap ( thread& other );
176 
177  pthread_t _thread;
178  pthread_attr_t _attr;
179 
180  thread_status _status;
181  };
182 
241  class abstract_thread: public runnable {
242  public:
248  explicit abstract_thread(const std::size_t stack_size = 0);
249  virtual ~abstract_thread();
250 
253  void start();
254 
259  void join() ;
260 
263  bool joinable() const ;
264 
265 
266  private:
267  pthread::thread *_thread;
268  std::size_t _stack_size ;
269  };
270 
295  public:
300 #if __cplusplus < 201103L
301  explicit thread_group( bool destructor_joins_first = false ) throw();
302 #else
303  explicit thread_group( bool destructor_joins_first = false ) noexcept;
304 #endif
305 
310  virtual ~thread_group();
311 
314  void add(abstract_thread *thread);
315 
320  void start();
321 
328  void join();
329 
333  unsigned long size();
334 
337  const bool destructor_joins_first(){ return _destructor_joins_first;};
338 
339  private:
340  std::list<pthread::abstract_thread*> _threads;
341  bool _destructor_joins_first;
342  };
343 
349  namespace this_thread{
357  void sleep_for(const int millis);
358 
360  pthread_t get_id() ;
362  }
363 
366 } // namespace pthread
367 
368 #endif /* thread_hpp */
const bool destructor_joins_first()
Definition: thread.hpp:337
bool joinable() const
Definition: thread.cpp:151
virtual ~thread()
Definition: thread.cpp:115
bool joinable() const
Definition: thread.hpp:139
void sleep_for(const int millis)
Definition: thread.cpp:17
void add(abstract_thread *thread)
Definition: thread.cpp:187
thread_status status()
Definition: thread.hpp:156
void * thread_startup_runnable(void *)
Definition: thread.cpp:215
virtual void run() noexcept=0
thread_status
Definition: thread.hpp:47
virtual ~thread_group()
Definition: thread.cpp:163
thread_group(bool destructor_joins_first=false) noexcept
Definition: thread.cpp:158
pthread_t get_id()
Definition: thread.cpp:21
void join()
Definition: thread.cpp:26
int cancel()
Definition: thread.cpp:59
abstract_thread(const std::size_t stack_size=0)
Definition: thread.cpp:132
thread & operator=(const thread &)=delete
unsigned long size()
Definition: thread.cpp:206