Helper class to represent threads of execution
An execution thread is a sequence of instructions that can run concurrently with other such sequences in multithreaded environments while sharing the same address space.
Is intended to reduce any code work of C++ on addons and to have them faster to use.
His code uses the support of platform-independent thread system introduced with C++11.
Example:
#include <kodi/tools/Thread.h>
#include <kodi/AddonBase.h>
class ATTRIBUTE_HIDDEN CTestAddon
{
public:
CTestAddon() = default;
void Process() override;
};
{
CreateThread();
Sleep(4000);
StopThread();
}
void CTestAddon::Process()
{
while (!m_threadStop)
{
Sleep(1000);
}
}
ADDONCREATOR(CTestAddon)
Definition: AddonBase.h:322
@ ADDON_LOG_INFO
1 : To include information messages in the log file.
Definition: addon_base.h:177
ADDON_STATUS
Definition: addon_base.h:128
@ ADDON_STATUS_OK
For everything OK and no error.
Definition: addon_base.h:130
void ATTRIBUTE_HIDDEN Log(const AddonLog loglevel, const char *format,...)
Add a message to Kodi's log.
Definition: AddonBase.h:749
◆ CThread()
◆ ~CThread()
◆ IsAutoDelete()
bool IsAutoDelete |
( |
| ) |
const |
|
inline |
Check auto delete is enabled on this thread class.
- Returns
- true if auto delete is used, false otherwise
◆ IsCurrentThread()
bool IsCurrentThread |
( |
| ) |
const |
|
inline |
Check caller is on this running thread.
- Returns
- true if called from thread inside the class, false if from another thread
◆ IsRunning()
Check thread inside this class is running and active.
- Note
- This function should be used from outside and not within process to check thread is active. Use use atomic bool m_threadStop for this.
- Returns
- true if running, false if not
◆ CreateThread()
void CreateThread |
( |
bool |
autoDelete = false | ) |
|
|
inline |
Create a new thread defined by this class on child.
This starts then Process() where is available on the child by addon.
- Parameters
-
[in] | autoDelete | To set thread to delete itself after end, default is false |
◆ StopThread()
void StopThread |
( |
bool |
wait = true | ) |
|
|
inline |
Stop a running thread.
- Parameters
-
[in] | wait | As true (default) to wait until thread is finished and stopped, as false the function return directly and thread becomes independently stopped. |
◆ Sleep()
void Sleep |
( |
uint32_t |
milliseconds | ) |
|
|
inline |
Thread sleep with given amount of milliseconds.
This makes a sleep in the thread with a given time value. If it is called within the process itself, it is also checked whether the thread is terminated and the sleep process is thereby interrupted.
If the external point calls this, only a regular sleep is used, which runs through completely.
- Parameters
-
[in] | milliseconds | Time to sleep |
◆ Join()
bool Join |
( |
unsigned int |
milliseconds | ) |
|
|
inline |
The function returns when the thread execution has completed or timing is reached in milliseconds beforehand.
This synchronizes the moment this function returns with the completion of all operations on the thread.
- Parameters
-
[in] | milliseconds | Time to wait for join |
◆ Process()
The function to be added by the addon as a child to carry out the process thread.
Use m_threadStop to check about active of thread and want stopped from external place.
- Note
- This function is necessary and must be implemented by the addon.
Implemented in CTimer.
◆ m_threadStop
std::atomic<bool> m_threadStop |
|
protected |
Atomic bool to indicate thread is active.
This should be used in Process() to check the activity of the thread and, if true, to terminate the process.
false
: Thread active and should be run
true
: Thread ends and should be stopped