PLCnext API Documentation 25.0.2.69
Thread.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include "Arp/System/Core/PimplPtr.hxx"
9#include "Arp/System/Core/delegate.hxx"
10#include "Arp/System/Commons/Threading/ThreadSettings.hpp"
11#include "Arp/System/Commons/Threading/ThreadState.hpp"
12
13#include "Arp/System/Commons/Logging.h"
14#include "Arp/System/Commons/Threading/AutoResetEvent.hpp"
15#include "Arp/System/Commons/Threading/ManualResetEvent.hpp"
16#include "Arp/System/Commons/Threading/Semaphore.hpp"
17#include "Arp/System/Commons/Threading/Mutex.hpp"
18#include <atomic>
19
21{
22
67class ARP_CXX_SYMBOL_EXPORT Thread
68{
69public: // usings
71 using ThreadStartFunction = void (*)(void*);
72
74 using ThreadStartDelegate = delegate<void(void*)>;
75
77 static const size_t CpuAffinityAll;
78
79public: // construction/destruction
80
87 template<class TInstance, class TFunction>
88 ARP_DEPRECATED("Use constructor with ThreadSettings instead. Supply at least a thread name!")
89 Thread(TInstance& instance, TFunction fn, void* pStartParam = nullptr);
90
97 template<class TInstance, class TFunction>
98 ARP_DEPRECATED("Use constructor with ThreadSettings instead. Supply at least a thread name!")
99 Thread(const TInstance& instance, TFunction fn, void* pStartParam = nullptr);
100
107 template<class TInstance, class TFunction>
108 ARP_DEPRECATED("Use constructor with ThreadSettings instead. Supply at least a thread name!")
109 Thread(TInstance* pInstance, TFunction fn, void* pStartParam = nullptr);
110
117 template<class TInstance, class TFunction>
118 ARP_DEPRECATED("Use constructor with ThreadSettings instead. Supply at least a thread name!")
119 Thread(const TInstance* pInstance, TFunction fn, void* pStartParam = nullptr);
120
126 ARP_DEPRECATED("Use constructor with ThreadSettings instead. Supply at least a thread name!")
127 explicit Thread(ThreadStartDelegate&& threadStart, void* pStartParam = nullptr);
128
138 ARP_DEPRECATED("Use constructor with ThreadSettings instead. Supply at least a thread name!")
139 Thread(ThreadStartFunction threadStart, void* pStartParam = nullptr);
140
147 template<class TInstance, class TFunction>
148 Thread(const ThreadSettings& settings, TInstance& instance, TFunction fn, void* pStartParam = nullptr);
149
156 template<class TInstance, class TFunction>
157 Thread(const ThreadSettings& settings, const TInstance& instance, TFunction fn, void* pStartParam = nullptr);
158
165 template<class TInstance, class TFunction>
166 Thread(const ThreadSettings& settings, TInstance* pInstance, TFunction fn, void* pStartParam = nullptr);
167
174 template<class TInstance, class TFunction>
175 Thread(const ThreadSettings& settings, const TInstance* pInstance, TFunction fn, void* pStartParam = nullptr);
176
182 Thread(ThreadSettings& settings, ThreadStartDelegate&& threadStart, void* pStartParam = nullptr);
183
189 Thread(ThreadSettings& settings, ThreadStartFunction threadStart, void* pStartParam = nullptr);
190
196 Thread(const ThreadSettings& settings, ThreadStartDelegate&& threadStart, void* pStartParam = nullptr);
197
203 Thread(const ThreadSettings& settings, ThreadStartFunction threadStart, void* pStartParam = nullptr);
204
205 // canonical construction/destruction/assignment
206
208 Thread(const Thread& arg) = delete;
209
211 Thread(Thread&& arg)noexcept;
212
214 Thread& operator=(const Thread& arg) = delete;
215
217 Thread& operator=(Thread&& arg)noexcept;
218
224 ~Thread(void);
225
226public: // static operations
227
230 static void Sleep(uint64 milliseconds);
231
234 static size_t GetCurrentThreadId(void);
235
238 static Thread* GetCurrentThread(void);
239
248 static void SetAsynchronousCancelability(bool enable);
249
250public: // properties/setter/getter
252 bool IsRunning(void)const;
253
271 void SetCpuAffinity(size_t mask);
272
276 size_t GetCpuAffinity(void)const;
277
281 void SetPriority(size_t value);
282
285 size_t GetPriority(void)const;
286
299 ARP_DEPRECATED("Set thread stack size at the constructor")
300 void SetStackSize(size_t value);
301
304 size_t GetStackSize(void)const;
305
313 void SetName(const String& value);
314
317 const String& GetName(void)const;
318
321 ThreadState GetState() const;
322
328 bool IsJoinable(void);
329
330public: // operations
333 void Start(void);
334
338 void Join(void);
339
345 void Interrupt(void);
346
348 void Terminate(void);
349
350public: // Impl forward declaration
351 class Impl;
352
353public: // internal operations
354 Impl& GetImpl(void);
355 const Impl& GetImpl(void)const;
356
357private: // Impl usings
358 using Pimpl = PimplPtr<Impl>;
359
360private: // Impl fields
361 Pimpl pimpl;
362};
363
365//inline methods of class Thread
366template<class TInstance, class TFunction>
367inline Thread::Thread(TInstance& instance, TFunction fn, void* pStartParam)
368 : Thread(ThreadSettings(""), (ThreadStartDelegate)make_delegate(&instance, fn), pStartParam)
369{
370}
371
372template<class TInstance, class TFunction>
373inline Thread::Thread(const TInstance& instance, TFunction fn, void* pStartParam)
374 : Thread(ThreadSettings(""), (ThreadStartDelegate)make_delegate(&instance, fn), pStartParam)
375{
376}
377
378template<class TInstance, class TFunction>
379inline Thread::Thread(TInstance* pInstance, TFunction fn, void* pStartParam)
380 : Thread(ThreadSettings(""), (ThreadStartDelegate)make_delegate(pInstance, fn), pStartParam)
381{
382}
383
384template<class TInstance, class TFunction>
385inline Thread::Thread(const TInstance* pInstance, TFunction fn, void* pStartParam)
386 : Thread(ThreadSettings(""), (ThreadStartDelegate)make_delegate(pInstance, fn), pStartParam)
387{
388}
389
390inline Thread::Thread(ThreadStartDelegate&& threadStart, void* pStartParam)
391 : Thread(ThreadSettings(""), std::move(threadStart), pStartParam)
392{
393}
394
395inline Thread::Thread(ThreadStartFunction threadStart, void* pStartParam)
396 : Thread(ThreadSettings(""), threadStart, pStartParam)
397{
398}
399
400template<class TInstance, class TFunction>
401inline Thread::Thread(const ThreadSettings& settings, TInstance& instance, TFunction fn, void* pStartParam)
402 : Thread(settings, (ThreadStartDelegate)make_delegate(&instance, fn), pStartParam)
403{
404}
405
406template<class TInstance, class TFunction>
407inline Thread::Thread(const ThreadSettings& settings, const TInstance& instance, TFunction fn, void* pStartParam)
408 : Thread(settings, (ThreadStartDelegate)make_delegate(&instance, fn), pStartParam)
409{
410}
411
412template<class TInstance, class TFunction>
413inline Thread::Thread(const ThreadSettings& settings, TInstance* pInstance, TFunction fn, void* pStartParam)
414 : Thread(settings, (ThreadStartDelegate)make_delegate(pInstance, fn), pStartParam)
415{
416}
417
418template<class TInstance, class TFunction>
419inline Thread::Thread(const ThreadSettings& settings, const TInstance* pInstance, TFunction fn, void* pStartParam)
420 : Thread(settings, (ThreadStartDelegate)make_delegate(pInstance, fn), pStartParam)
421{
422}
423
424inline Thread::Thread(ThreadSettings& settings, ThreadStartDelegate&& threadStart, void* pStartParam)
425 : Thread((const ThreadSettings&)settings, std::move(threadStart), pStartParam)
426{
427}
428
429inline Thread::Thread(ThreadSettings& settings, ThreadStartFunction threadStart, void* pStartParam)
430 : Thread((const ThreadSettings&)settings, threadStart, pStartParam)
431{
432}
433
434} // end of namespace Arp::System::Commons::Threading
Adapter class to implement PImpl idiom.
Definition: PimplPtr.hxx:15
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
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
static const size_t CpuAffinityAll
Use this constant to express an affinity of the thread to all available CPUs aka. cores.
Definition: Thread.hpp:77
Thread(TInstance &instance, TFunction fn, void *pStartParam=nullptr)
Deprecated! Constructs a Thread instance for a class method.
Definition: Thread.hpp:367
void(*)(void *) ThreadStartFunction
Definition of signature of function to be executed in a separate thread.
Definition: Thread.hpp:71
std::uint64_t uint64
The Arp unsigned integer type of 8 byte size.
Definition: PrimitiveTypes.hpp:37
Namespace for classes handling threads and synchronization
ThreadState
Possible thread states.
Definition: ThreadState.hpp:15
class ARP_DEPRECATED("Use Arp::Enum<T> instead.") EnumStrings
Deprecated! The class implements an adapter for enums to define the string literals of the enum entri...
Definition: EnumStrings.hxx:38
Namespace of the C++ standard library