Kodi Development  20.0
for Binary and Script based Add-Ons

Detailed Description

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;
ADDON_STATUS Create() override;
void Process() override;
};
ADDON_STATUS CTestAddon::Create()
{
kodi::Log(ADDON_LOG_INFO, "Starting thread");
CreateThread();
Sleep(4000);
kodi::Log(ADDON_LOG_INFO, "Stopping thread");
// This added as example and also becomes stopped by class destructor
StopThread();
}
void CTestAddon::Process()
{
kodi::Log(ADDON_LOG_INFO, "Thread started");
while (!m_threadStop)
{
kodi::Log(ADDON_LOG_INFO, "Hello World");
Sleep(1000);
}
kodi::Log(ADDON_LOG_INFO, "Thread ended");
}
ADDONCREATOR(CTestAddon)
Definition: AddonBase.h:322
Definition: Thread.h:91
@ 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

Variables

std::atomic< bool > m_threadStop
 Atomic bool to indicate thread is active. More...
 

Function Documentation

◆ CThread()

CThread ( )
inline

Class constructor.

◆ ~CThread()

virtual ~CThread ( )
inlinevirtual

Class destructor.

◆ 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()

bool IsRunning ( ) const
inline

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]autoDeleteTo set thread to delete itself after end, default is false

◆ StopThread()

void StopThread ( bool  wait = true)
inline

Stop a running thread.

Parameters
[in]waitAs 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]millisecondsTime 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]millisecondsTime to wait for join

◆ Process()

virtual void Process ( )
protectedpure virtual

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.

Variable Documentation

◆ 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