PLCnext API Documentation 25.0.2.69
WorkerThread.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include "Arp/System/Commons/Threading/Thread.hpp"
9
10namespace Arp { namespace System { namespace Commons { namespace Threading
11{
12
15{
16public: // usings
18 using WorkDelegate = delegate<void(void)>;
19
20public: // construction/destruction
21
22 template<class TInstance, class TFunction>
23 WorkerThread(TInstance& instance, TFunction fn, int idleTime, const char* threadName);
24
25 template<class TInstance, class TFunction>
26 WorkerThread(const TInstance& instance, TFunction fn, int idleTime, const char* threadName);
27
28 template<class TInstance, class TFunction>
29 WorkerThread(TInstance* pInstance, TFunction fn, int idleTime, const char* threadName);
30
31 template<class TInstance, class TFunction>
32 WorkerThread(const TInstance* pInstance, TFunction fn, int idleTime, const char* threadName);
33
34 template<class TInstance, class TFunction>
35 WorkerThread(ThreadSettings& settings, TInstance& instance, TFunction fn, int idleTime);
36
37 template<class TInstance, class TFunction>
38 WorkerThread(ThreadSettings& settings, const TInstance& instance, TFunction fn, int idleTime);
39
40 template<class TInstance, class TFunction>
41 WorkerThread(ThreadSettings& settings, TInstance* pInstance, TFunction fn, int idleTime);
42
43 template<class TInstance, class TFunction>
44 WorkerThread(ThreadSettings& settings, const TInstance* pInstance, TFunction fn, int idleTime);
45
46 WorkerThread(WorkDelegate&& loopDelegate, int idleTime, const char* threadName);
47 WorkerThread(ThreadSettings& settings, WorkDelegate&& loopDelegate, int idleTime);
48
50 ~WorkerThread(void) = default;
51
52public: // setter/getter operations
53
54 bool IsRunning(void)const;
55
56public: // operations
57
58 void Start(void);
59
60 void Stop(void);
61
62private: // static methods
63 static void RunInternal(void* pParam);
64
65private: // fields
66 Thread thread;
67 WorkDelegate work;
68 std::atomic<bool> stopWorking{false};
69 std::atomic<bool> isStarted{false};
70 int idleTime = 1;
71};
72
74// inline methods of class WorkerThread
75
82template<class TInstance, class TFunction>
83inline WorkerThread::WorkerThread(TInstance& instance, TFunction fn, int idleTime, const char* threadName)
84 : WorkerThread(&instance, fn, idleTime, threadName)
85{
86}
87
94template<class TInstance, class TFunction>
95inline WorkerThread::WorkerThread(const TInstance& instance, TFunction fn, int idleTime, const char* threadName)
96 : WorkerThread(&instance, fn, idleTime, threadName)
97{
98}
99
106template<class TInstance, class TFunction>
107inline WorkerThread::WorkerThread(TInstance* pInstance, TFunction fn, int idleTime, const char* threadName)
108 : thread(ThreadSettings(threadName), &WorkerThread::RunInternal, (void*)this)
109 , idleTime(idleTime)
110 , work(make_delegate(pInstance, fn))
111{
112}
113
120template<class TInstance, class TFunction>
121inline WorkerThread::WorkerThread(const TInstance* pInstance, TFunction fn, int idleTime, const char* threadName)
122 : thread(ThreadSettings(threadName), &WorkerThread::RunInternal, (void*)this)
123 , idleTime(idleTime)
124 , work(make_delegate(pInstance, fn))
125{
126}
127
134template<class TInstance, class TFunction>
135inline WorkerThread::WorkerThread(ThreadSettings& settings, TInstance& instance, TFunction fn, int idleTimeArg)
136 : WorkerThread(settings, &instance, fn, idleTime)
137{
138}
139
146template<class TInstance, class TFunction>
147inline WorkerThread::WorkerThread(ThreadSettings& settings, const TInstance& instance, TFunction fn, int idleTimeArg)
148 : WorkerThread(settings, &instance, fn, idleTime)
149{
150}
151
158template<class TInstance, class TFunction>
159inline WorkerThread::WorkerThread(ThreadSettings& settings, TInstance* pInstance, TFunction fn, int idleTimeArg)
160 : thread(settings, &WorkerThread::RunInternal, (void*)this)
161 , idleTime(idleTimeArg)
162 , work(make_delegate(pInstance, fn))
163{
164}
165
172template<class TInstance, class TFunction>
173inline WorkerThread::WorkerThread(ThreadSettings& settings, const TInstance* pInstance, TFunction fn, int idleTimeArg)
174 : thread(settings, &WorkerThread::RunInternal, (void*)this)
175 , idleTime(idleTimeArg)
176 , work(make_delegate(pInstance, fn))
177{
178}
179
180}}}} // end of namespace Arp::System::Commons::Threading
Prototyping of delegate template.
Definition: delegate.hxx:14
Container class for adaptable thread settings.
Definition: ThreadSettings.hpp:13
The Thread-class provides methods to execute functions and methods in a separate thread.
Definition: Thread.hpp:68
Worker threads repeat the execution of the threaded code until Stop is called.
Definition: WorkerThread.hpp:15
bool IsRunning(void) const
Checks if the thread is in running state.
Definition: WorkerThread.cpp:80
void Start(void)
Starts the execution of the thread.
Definition: WorkerThread.cpp:37
void Stop(void)
Stops the execution of the thread syncronously.
Definition: WorkerThread.cpp:53
~WorkerThread(void)=default
Deallocates the memory used for this instance but does not quit the thread.
WorkerThread(TInstance &instance, TFunction fn, int idleTime, const char *threadName)
Constructs an WorkerThread instance for a class method.
Definition: WorkerThread.hpp:83
Root namespace for the PLCnext API