ADTF
thread_looper.h
Go to the documentation of this file.
1 
7 #ifndef _A_UTILS_THREAD_LOOPER_HEADER_
8 #define _A_UTILS_THREAD_LOOPER_HEADER_
9 
10 #include <mutex>
11 #include <future>
12 
13 namespace A_UTILS_NS
14 {
15 
16 class cThreadLooper: public std::thread, public cLooper
17 {
18  public:
19  cThreadLooper() {}
20  cThreadLooper(const cThreadLooper&) = delete;
21 
22  template<typename ...Args>
23  cThreadLooper(Args&&... args):
24  cLooper(std::forward<Args>(args)...)
25  {
26  *static_cast<std::thread*>(this) = std::thread(GetCallable());
27  }
28 
29  cThreadLooper(cThreadLooper&& oOther):
30  thread(static_cast<thread&&>(oOther)),
31  cLooper(static_cast<cLooper&&>(oOther))
32  {
33  }
34 
35  cThreadLooper& operator=(cThreadLooper&& oOther)
36  {
37  thread::swap(oOther);
38  cLooper::Swap(oOther);
39  return *this;
40  }
41 
42  ~cThreadLooper()
43  {
44  if (joinable())
45  {
47  join();
48  }
49  }
50 };
51 
53 {
54  public:
55  cThreadLooperWithScheduling() = delete;
58 
59  template<typename ...Args>
60  cThreadLooperWithScheduling(const tSchedulingParameters& sScheduling, Args&&... args):
62  {
63  *static_cast<cLooper*>(this) = cLooper(std::forward<Args>(args)...);
64  *static_cast<std::thread*>(this) = create_thread_with_scheduling(sScheduling, GetCallable());
65  }
66 };
67 
69 {
70  private:
71  cThreadLooper m_oLooper;
72  tBool m_bSelfDestruct;
73 
74 #ifdef _DEBUG
75  cString m_strName;
76 #endif
77 
78  public:
79  virtual ~cThreadLoop();
80  tResult Create(tBool bSelfDestruct = tFalse);
81  tResult Create(const tSchedulingParameters& sScheduling, tBool bSelfDestruct = tFalse);
82  tResult Release();
83  cLooper::tLoopState GetState();
84  tResult SetState(cLooper::tLoopState eState, tBool bWaitForAck = tTrue);
85 
86 
87 #ifdef _DEBUG
88  tVoid SetName(const tChar* strName);
89 #endif // _DEBUG
90 
91  protected:
92  virtual tResult LoopFunc() = 0;
93 
94  private:
95  void Loop();
96 
97  private:
98  tVoid SelfDestruct();
99 
100 };
101 
102 }
103 
104 #endif
char tChar
The tChar defines the type for platform character set (platform and compiler dependent type).
void tVoid
The tVoid is always the definition for the void (non-type).
bool tBool
The tBool defines the type for the Values tTrue and tFalse (platform and compiler dependent).
The looper helper class is a managed endless loop, which can be suspended.
Definition: looper.h:21
tLoopState
Loop state.
Definition: looper.h:27
@ Destroyed
loop is finished or only constructed
#define tFalse
Value for tBool.
Definition: constants.h:60
#define tTrue
Value for tBool.
Definition: constants.h:62
ADTF A_UTIL Namespace - Within adtf this is used as adtf::util or adtf_util.
Definition: d_ptr.h:11
tResult create_thread_with_scheduling(std::thread &oThread, const tSchedulingParameters &sScheduling, Callable &&pFunc, Args &&... args)
Creates a new thread that has the given scheduling parameters.
std::string join(const std::vector< std::string > &strings, const std::string &delimiter)
Joins all strings from the vector into one string, separated by the specified delimiter string.
Compound class, that groups scheduling parameters.