PLCnext API Documentation
22.9.0.33
|
The Thread-class provides methods to execute functions and methods in a separate thread. More...
#include <Thread.hpp>
Public Types | |
typedef void(* | ThreadStartFunction) (void *) |
Definition of signature of function to be executed in a separate thread. More... | |
typedef delegate< void(void *)> | ThreadStartDelegate |
Definition of signature of class method to be executed in a separate thread. More... | |
Public Member Functions | |
template<class TInstance , class TFunction > | |
Thread (TInstance &instance, TFunction fn, void *pStartParam=nullptr) | |
Constructs a Thread instance for a class method. More... | |
template<class TInstance , class TFunction > | |
Thread (const TInstance &instance, TFunction fn, void *pStartParam=nullptr) | |
Constructs a Thread instance for a class method. More... | |
template<class TInstance , class TFunction > | |
Thread (TInstance *pInstance, TFunction fn, void *pStartParam=nullptr) | |
Constructs a Thread instance for a class method. More... | |
template<class TInstance , class TFunction > | |
Thread (const TInstance *pInstance, TFunction fn, void *pStartParam=nullptr) | |
Constructs a Thread instance for a class method. More... | |
Thread (ThreadStartDelegate &&threadStart, void *pStartParam=nullptr) | |
Constructs an Thread instance for a class method with single parameter. More... | |
Thread (ThreadStartFunction threadStart, void *pStartParam=nullptr) | |
Constructs a Thread instance for a function with single parameter. More... | |
template<class TInstance , class TFunction > | |
Thread (const ThreadSettings &settings, TInstance &instance, TFunction fn, void *pStartParam=nullptr) | |
Like Thread(TInstance instance, TFunction fn) but with additional thread parameters. More... | |
template<class TInstance , class TFunction > | |
Thread (const ThreadSettings &settings, const TInstance &instance, TFunction fn, void *pStartParam=nullptr) | |
Like Thread(TInstance instance, TFunction fn) but with additional thread parameters. More... | |
template<class TInstance , class TFunction > | |
Thread (const ThreadSettings &settings, TInstance *pInstance, TFunction fn, void *pStartParam=nullptr) | |
Like Thread(TInstance instance, TFunction fn) but with additional thread parameters. More... | |
template<class TInstance , class TFunction > | |
Thread (const ThreadSettings &settings, const TInstance *pInstance, TFunction fn, void *pStartParam=nullptr) | |
Like Thread(TInstance instance, TFunction fn) but with additional thread parameters. More... | |
Thread (ThreadSettings &settings, ThreadStartDelegate &&threadStart, void *pStartParam=nullptr) | |
Like Thread(ThreadStartDelegate&& threadStart, void* pStartParam = nullptr) but with additional thread parameters. More... | |
Thread (ThreadSettings &settings, ThreadStartFunction threadStart, void *pStartParam=nullptr) | |
Like Thread(ThreadStartFunction threadStart, void* pStartParam = nullptr) but with additional thread parameters. More... | |
Thread (const ThreadSettings &settings, ThreadStartDelegate &&threadStart, void *pStartParam=nullptr) | |
Like Thread(ThreadStartDelegate&& threadStart, void* pStartParam = nullptr) but with additional thread parameters. More... | |
Thread (const ThreadSettings &settings, ThreadStartFunction threadStart, void *pStartParam=nullptr) | |
Like Thread(ThreadStartFunction threadStart, void* pStartParam = nullptr) but with additional thread parameters. More... | |
~Thread (void) | |
Destructs this instance and frees all resources. More... | |
bool | IsRunning (void) const |
Determines if this thread is in running state. More... | |
void | SetCpuAffinity (size_t mask) |
Pins the thread to a specific CPUs inside a multiprocessor system. More... | |
size_t | GetCpuAffinity (void) const |
Returns the CPU affinity mask. More... | |
void | SetPriority (size_t value) |
Assigns a new priority to the thread. More... | |
size_t | GetPriority (void) const |
Returns the current priority of the thread. More... | |
void | SetStackSize (size_t value) |
Sets a new stack size for the thread. More... | |
size_t | GetStackSize (void) const |
Returns the current stack size of the thread.
| |
void | SetName (const String &value) |
Sets a new name to the thread. More... | |
const String & | GetName (void) const |
Returns the current name of the thread. More... | |
ThreadState | GetState () const |
Returns the current state of the thread. More... | |
bool | IsJoinable (void) |
Checks if this thread is joinable. More... | |
void | Start (void) |
Starts the execution of the thread. More... | |
void | Join (void) |
Waits for the thread to terminate. More... | |
void | Interrupt (void) |
Interrupts the currents execution. More... | |
void | Terminate (void) |
Aborts the execution of this thread. More... | |
Static Public Member Functions | |
static void | Sleep (size_t milliseconds) |
Suspends the execution of the calling thread. More... | |
static size_t | GetCurrentThreadId (void) |
Returns the id of the calling thread. More... | |
static Thread * | GetCurrentThread (void) |
Returns the current thread of the calling context. More... | |
static void | SetAsynchronousCancelability (bool enable) |
Activates/Deactivates the asynchronous cancelability of the calling thread. More... | |
Static Public Attributes | |
static const std::size_t | CpuAffinityAll |
Use this constant to express an affinity of the thread to all available CPUs aka. cores. More... | |
Friends | |
class | AutoResetEvent |
class | AutoResetEvent2 |
The Thread-class provides methods to execute functions and methods in a separate thread.
An instance of this class is used to manage one single thread of execution. The function or method to be executed in a separate thread must thereby be provided during object instance creation. The execution is not started until Arp::System::Commons::Threading::Thread::Start is called on the newly created instance.
Consider the following class
If the class-method ThreadWorker::Run has to be executed in an extra thread of execution this can be done with the following code.
Several other constructors exists to support different use-cases of threaded execution. For example, if the method ThreadWorker::Run also needs an additional parameter to work properly a corresponding thread object can be created using the following lines of code.
See api documentation for more information how to create thread object instances.
typedef delegate<void(void*)> Arp::System::Commons::Threading::Thread::ThreadStartDelegate |
Definition of signature of class method to be executed in a separate thread.
typedef void(* Arp::System::Commons::Threading::Thread::ThreadStartFunction) (void *) |
Definition of signature of function to be executed in a separate thread.
|
inline |
Constructs a Thread instance for a class method.
instance | Instance of object whichs method should be executed in separate thread. |
fn | Pointer to method that should be executed in separate thread. |
pStartParam | Pointer to optional parameter passed to function threadStart. |
Arp::System::Commons::InvalidOperationException | if the thread could not be created. |
|
inline |
Constructs a Thread instance for a class method.
instance | Instance of object whichs method should be executed in separate thread. |
fn | Pointer to method that should be executed in separate thread. |
pStartParam | Pointer to optional parameter passed to function threadStart. |
Arp::System::Commons::InvalidOperationException | if the thread could not be created. |
|
inline |
Constructs a Thread instance for a class method.
pInstance | Instance of object whichs method should be executed in separate thread. |
fn | Pointer to method that should be executed in separate thread. |
pStartParam | Pointer to optional parameter passed to function threadStart. |
Arp::System::Commons::InvalidOperationException | if the thread could not be created. |
|
inline |
Constructs a Thread instance for a class method.
pInstance | Instance of object whichs method should be executed in separate thread. |
fn | Pointer to method that should be executed in separate thread. |
pStartParam | Pointer to optional parameter passed to function threadStart. |
Arp::System::Commons::InvalidOperationException | if the thread could not be created. |
|
inlineexplicit |
Constructs an Thread instance for a class method with single parameter.
threadStart | A delegate of a class method and a corresponding instance created using Arp::make_delegate |
pStartParam | Pointer to optional parameter passed to class method referenced in the constructed delegate. |
Arp::System::Commons::InvalidOperationException | if the thread could not be created. |
|
inline |
Constructs a Thread instance for a function with single parameter.
Example:
threadStart | Function to be executed. |
pStartParam | Pointer to optional parameter passed to function threadStart. |
Arp::System::Commons::InvalidOperationException | if the thread could not be created. |
|
inline |
Like Thread(TInstance instance, TFunction fn) but with additional thread parameters.
settings | Reference to custom thread parameters. |
instance | Instance of object whichs method should be executed in separate thread. |
fn | Pointer to method that should be executed in separate thread. |
pStartParam | Optional parameter passed to function fn during invocation. |
Arp::System::Commons::InvalidOperationException | if the thread could not be created. |
|
inline |
Like Thread(TInstance instance, TFunction fn) but with additional thread parameters.
settings | Reference to custom thread parameters. |
instance | Instance of object whichs method should be executed in separate thread. |
fn | Pointer to method that should be executed in separate thread. |
pStartParam | Optional parameter passed to function fn during invocation. |
Arp::System::Commons::InvalidOperationException | if the thread could not be created. |
|
inline |
Like Thread(TInstance instance, TFunction fn) but with additional thread parameters.
settings | Reference to custom thread parameters. |
pInstance | Pointer to instance of object whichs method should be executed in separate thread. |
fn | Pointer to method that should be executed in separate thread. |
pStartParam | Optional parameter passed to function fn during invocation. |
Arp::System::Commons::InvalidOperationException | if the thread could not be created. |
|
inline |
Like Thread(TInstance instance, TFunction fn) but with additional thread parameters.
settings | Reference to custom thread parameters. |
pInstance | Pointer to instance of object whichs method should be executed in separate thread. |
fn | Pointer to method that should be executed in separate thread. |
pStartParam | Optional parameter passed to function fn during invocation. |
Arp::System::Commons::InvalidOperationException | if the thread could not be created. |
|
inline |
Like Thread(ThreadStartDelegate&& threadStart, void* pStartParam = nullptr) but with additional thread parameters.
settings | Reference to custom thread parameters. |
threadStart | Delegate of class instance and method to be executed in separate thread. |
pStartParam | Optional parameter passed to methode in delegate threadStart during invocation. |
Arp::System::Commons::InvalidOperationException | if the thread could not be created. |
|
inline |
Like Thread(ThreadStartFunction threadStart, void* pStartParam = nullptr) but with additional thread parameters.
settings | Reference to custom thread parameters. |
threadStart | Function to be executed in separate thread. |
pStartParam | Optional parameter passed to function threadStart during invocation. |
Arp::System::Commons::InvalidOperationException | if the thread could not be created. |
|
inline |
Like Thread(ThreadStartDelegate&& threadStart, void* pStartParam = nullptr) but with additional thread parameters.
settings | Reference to custom thread parameters. |
threadStart | Delegate of class instance and method to be executed in separate thread. |
pStartParam | Optional parameter passed to methode in delegate threadStart during invocation. |
Arp::System::Commons::InvalidOperationException | if the thread could not be created. |
|
inline |
Like Thread(ThreadStartFunction threadStart, void* pStartParam = nullptr) but with additional thread parameters.
settings | Reference to custom thread parameters. |
threadStart | Function to be executed in separate thread. |
pStartParam | Optional parameter passed to function threadStart during invocation. |
Arp::System::Commons::InvalidOperationException | if the thread could not be created. |
Arp::System::Commons::Threading::Thread::~Thread | ( | void | ) |
Destructs this instance and frees all resources.
This will not stop the underlying thread from running. To prevent the creation of zombie threads Terminate must be called before the object destructor is invoked.
size_t Arp::System::Commons::Threading::Thread::GetCpuAffinity | ( | void | ) | const |
|
inlinestatic |
Returns the current thread of the calling context.
nullptr
if the calling thread is not a Arp thread (e.g. main thread).
|
static |
Returns the id of the calling thread.
const String& Arp::System::Commons::Threading::Thread::GetName | ( | void | ) | const |
Returns the current name of the thread.
size_t Arp::System::Commons::Threading::Thread::GetPriority | ( | void | ) | const |
Returns the current priority of the thread.
|
inline |
Returns the current state of the thread.
|
inline |
Interrupts the currents execution.
If this thread is currently waiting on an event to be signaled, the waiting is interrupted and an exception is thrown inside the thread signalling the interruption of the current execution.
bool Arp::System::Commons::Threading::Thread::IsJoinable | ( | void | ) |
|
inline |
Determines if this thread is in running state.
void Arp::System::Commons::Threading::Thread::Join | ( | void | ) |
Waits for the thread to terminate.
The thread must be joinable for this method to work properly. See #Joinable for more information.
|
static |
Activates/Deactivates the asynchronous cancelability of the calling thread.
The default state for all new threads is that a cancellation request send by Arp::System::Commons::Threading::Thread::Terminate is deferred until the thread reaches the next cancellation point. A cancellation point is a call to function like, for example, Arp::System::Commons::Net::Socket::Accept. If a thread activates asynchronous cancelability then the thread can be canceled at any time using Arp::System::Commons::Threading::Thread::Terminate.
enable | Set to true to activate asynchronous cancelability, otherwise set to false. |
void Arp::System::Commons::Threading::Thread::SetCpuAffinity | ( | size_t | mask | ) |
Pins the thread to a specific CPUs inside a multiprocessor system.
The assigned parameter is interpreted as a bit-mask to determine which CPUs are allowed to run the specific thread. So, for example, if the thread should run on the first and second CPU set mask = 3.
void Arp::System::Commons::Threading::Thread::SetName | ( | const String & | value | ) |
Sets a new name to the thread.
The maximal length of the new name depends on the operating system. If the assigned char sequence is too long it is automatically truncated to this maximal allowed length.
value | New name of the thread. |
Arp::Exception | with additional error information. |
void Arp::System::Commons::Threading::Thread::SetPriority | ( | size_t | value | ) |
Assigns a new priority to the thread.
value | The new priority value. |
Arp::Exception | with additional error information. |
void Arp::System::Commons::Threading::Thread::SetStackSize | ( | size_t | value | ) |
Sets a new stack size for the thread.
The stack size can only be set if the thread is not started , i.e. Start was not called yet.
value | Size in bytes for requested stack. |
Arp::Exception | with additional error information. |
<deprecated> This method throws an exception now and will be removed in the future. The reason therefore is an modification of the thread startup behavior. The stack size can be set with the thread settings at the constructor. </deprecated>
|
static |
Suspends the execution of the calling thread.
milliseconds | Amount of milliseconds the execution of the calling thread is suspended. |
void Arp::System::Commons::Threading::Thread::Start | ( | void | ) |
Starts the execution of the thread.
Arp::Exception | with additional error information. |
void Arp::System::Commons::Threading::Thread::Terminate | ( | void | ) |
Aborts the execution of this thread.
|
static |
Use this constant to express an affinity of the thread to all available CPUs aka. cores.