PLCnext API Documentation  20.6.0.30321
WorkerThread.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/Arp.h"
8 #include "Arp/System/Commons/Threading/Thread.hpp"
9 
10 namespace Arp { namespace System { namespace Commons { namespace Threading
11 {
12 
15 {
16 public: // typedefs
18  typedef delegate<void(void)> WorkDelegate;
19 
20 public: // construction/destruction
21 
28  template<class TInstance, class TFunction>
29  WorkerThread(TInstance& instance, TFunction fn, int idleTime, const char* threadName);
30 
37  template<class TInstance, class TFunction>
38  WorkerThread(const TInstance& instance, TFunction fn, int idleTime, const char* threadName);
39 
46  template<class TInstance, class TFunction>
47  WorkerThread(TInstance* pInstance, TFunction fn, int idleTime, const char* threadName);
48 
55  template<class TInstance, class TFunction>
56  WorkerThread(const TInstance* pInstance, TFunction fn, int idleTime, const char* threadName);
57 
64  template<class TInstance, class TFunction>
65  WorkerThread(ThreadSettings& settings, TInstance& instance, TFunction fn, int idleTime);
66 
73  template<class TInstance, class TFunction>
74  WorkerThread(ThreadSettings& settings, const TInstance& instance, TFunction fn, int idleTime);
75 
82  template<class TInstance, class TFunction>
83  WorkerThread(ThreadSettings& settings, TInstance* pInstance, TFunction fn, int idleTime);
84 
91  template<class TInstance, class TFunction>
92  WorkerThread(ThreadSettings& settings, const TInstance* pInstance, TFunction fn, int idleTime);
93 
99  explicit WorkerThread(WorkDelegate&& loopDelegate, int idleTime, const char* threadName);
100 
106  WorkerThread(ThreadSettings& settings, WorkDelegate&& loopDelegate, int idleTime);
107 
109  ~WorkerThread(void) = default;
110 
111 public: // setter/getter operations
112 
115  bool IsRunning(void)const;
116 
117 public: // operations
119  void Start(void);
120 
122  void Stop(void);
123 
124 private: // static methods
125  static void RunInternal(void* pParam);
126 
127 private: // fields
128  Thread thread;
129  WorkDelegate work;
130  std::atomic<bool> stopWorking{false};
131  std::atomic<bool> isStarted{false};
132  int idleTime = 1;
133 };
134 
136 // inline methods of class WorkerThread
137 
138 template<class TInstance, class TFunction>
139 inline WorkerThread::WorkerThread(TInstance& instance, TFunction fn, int idleTime, const char* threadName)
140  : WorkerThread(&instance, fn, idleTime, threadName)
141 {
142 }
143 
144 template<class TInstance, class TFunction>
145 inline WorkerThread::WorkerThread(const TInstance& instance, TFunction fn, int idleTime, const char* threadName)
146  : WorkerThread(&instance, fn, idleTime, threadName)
147 {
148 }
149 
150 template<class TInstance, class TFunction>
151 inline WorkerThread::WorkerThread(TInstance* pInstance, TFunction fn, int idleTime, const char* threadName)
152  : thread(ThreadSettings(threadName), &WorkerThread::RunInternal, (void*)this)
153  , idleTime(idleTime)
154  , work(make_delegate(pInstance, fn))
155 {
156 }
157 
158 template<class TInstance, class TFunction>
159 inline WorkerThread::WorkerThread(const TInstance* pInstance, TFunction fn, int idleTime, const char* threadName)
160  : thread(ThreadSettings(threadName), &WorkerThread::RunInternal, (void*)this)
161  , idleTime(idleTime)
162  , work(make_delegate(pInstance, fn))
163 {
164 }
165 
166 inline WorkerThread::WorkerThread(WorkDelegate&& workDelegate, int idleTime, const char* threadName)
167  : thread(ThreadSettings(threadName), &WorkerThread::RunInternal, (void*)this)
168  , idleTime(idleTime)
169  , work(workDelegate)
170 {
171 }
172 
173 template<class TInstance, class TFunction>
174 inline WorkerThread::WorkerThread(ThreadSettings& settings, TInstance& instance, TFunction fn, int idleTimeArg)
175  : WorkerThread(settings, &instance, fn, idleTime)
176 {
177 }
178 
179 template<class TInstance, class TFunction>
180 inline WorkerThread::WorkerThread(ThreadSettings& settings, const TInstance& instance, TFunction fn, int idleTimeArg)
181  : WorkerThread(settings, &instance, fn, idleTime)
182 {
183 }
184 
185 template<class TInstance, class TFunction>
186 inline WorkerThread::WorkerThread(ThreadSettings& settings, TInstance* pInstance, TFunction fn, int idleTimeArg)
187  : thread(settings, &WorkerThread::RunInternal, (void*)this)
188  , idleTime(idleTimeArg)
189  , work(make_delegate(pInstance, fn))
190 {
191 }
192 
193 template<class TInstance, class TFunction>
194 inline WorkerThread::WorkerThread(ThreadSettings& settings, const TInstance* pInstance, TFunction fn, int idleTimeArg)
195  : thread(settings, &WorkerThread::RunInternal, (void*)this)
196  , idleTime(idleTimeArg)
197  , work(make_delegate(pInstance, fn))
198 {
199 }
200 
201 inline WorkerThread::WorkerThread(ThreadSettings& settings, WorkDelegate&& workDelegate, int idleTimeArg)
202  : thread(settings, &WorkerThread::RunInternal, (void*)this)
203  , idleTime(idleTimeArg)
204  , work(workDelegate)
205 {
206 }
207 
209 inline bool WorkerThread::IsRunning(void)const
210 {
211  return this->thread.IsRunning();
212 }
213 
214 }}}} // end of namespace Arp::System::Commons::Threading
delegate< R(A...)> make_delegate(R(*const function_ptr)(A...)) noexcept
Creates a delegate from a static function.
Definition: delegate.hxx:215
void Start(void)
Starts the execution of the thread.
Worker threads repeat the execution of the threaded code until Stop is called.
Definition: WorkerThread.hpp:14
~WorkerThread(void)=default
Deallocates the memory used for this instance but does not quit the thread.
The Thread-class provides methods to execute functions and methods in a separate thread.
Definition: Thread.hpp:91
void Stop(void)
Stops the execution of the thread syncronously.
bool IsRunning(void) const
Determines if this thread is in running state.
Definition: Thread.hpp:612
bool IsRunning(void) const
Checks if the thread is in running state.
Definition: WorkerThread.hpp:209
delegate< void(void)> WorkDelegate
The delegate of threads loop body.
Definition: WorkerThread.hpp:18
Container class for adaptable thread settings.
Definition: ThreadSettings.hpp:12
Root namespace for the PLCnext API
WorkerThread(TInstance &instance, TFunction fn, int idleTime, const char *threadName)
Constructs an WorkerThread instance for a class method.
Definition: WorkerThread.hpp:139
System components used by the System, Device, Plc or Io domains.