Kodi Documentation 22.0
Kodi is an open source media player and entertainment hub.
Loading...
Searching...
No Matches

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. More...

Classes

class  kodi::tools::CThread
 

Functions

 kodi::tools::CThread::CThread ()
 Class constructor.
 
virtual kodi::tools::CThread::~CThread ()
 Class destructor.
 
bool kodi::tools::CThread::IsAutoDelete () const
 Check auto delete is enabled on this thread class.
 
bool kodi::tools::CThread::IsCurrentThread () const
 Check caller is on this running thread.
 
bool kodi::tools::CThread::IsRunning () const
 Check thread inside this class is running and active.
 
void kodi::tools::CThread::CreateThread (bool autoDelete=false)
 Create a new thread defined by this class on child.
 
void kodi::tools::CThread::StopThread (bool wait=true)
 Stop a running thread.
 
void kodi::tools::CThread::Sleep (uint32_t milliseconds)
 Thread sleep with given amount of milliseconds.
 
bool kodi::tools::CThread::Join (unsigned int milliseconds)
 The function returns when the thread execution has completed or timing is reached in milliseconds beforehand.
 
virtual void kodi::tools::CThread::Process ()=0
 The function to be added by the addon as a child to carry out the process thread.
 

Variables

std::atomic< boolkodi::tools::CThread::m_threadStop
 Atomic bool to indicate thread is active.
 

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/AddonBase.h>
class ATTR_DLL_LOCAL 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)
#define ATTR_DLL_LOCAL
Definition addon_base.h:86
Definition kodi-dev-kit/include/kodi/AddonBase.h:775
Definition addons/kodi-dev-kit/include/kodi/tools/Thread.h:91
@ ADDON_LOG_INFO
1 : To include information messages in the log file.
Definition addon_base.h:187
ADDON_STATUS
Definition addon_base.h:138
@ ADDON_STATUS_OK
For everything OK and no error.
Definition addon_base.h:140
virtual ADDON_STATUS Create()
Main addon creation function.
Definition kodi-dev-kit/include/kodi/AddonBase.h:817
virtual void Process()=0
The function to be added by the addon as a child to carry out the process thread.
void ATTR_DLL_LOCAL Log(const ADDON_LOG loglevel, const char *format,...)
Add a message to Kodi's log.
Definition kodi-dev-kit/include/kodi/AddonBase.h:1938
#define ADDONCREATOR(AddonClass)
Definition kodi-dev-kit/include/kodi/AddonBase.h:2047
void Sleep(std::chrono::duration< Rep, Period > duration)
Definition XTimeUtils.h:68

Function Documentation

◆ CreateThread()

void kodi::tools::CThread::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

◆ CThread()

kodi::tools::CThread::CThread ( )
inline

Class constructor.

◆ IsAutoDelete()

bool kodi::tools::CThread::IsAutoDelete ( ) const
inline

Check auto delete is enabled on this thread class.

Returns
true if auto delete is used, false otherwise

◆ IsCurrentThread()

bool kodi::tools::CThread::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 kodi::tools::CThread::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

◆ Join()

bool kodi::tools::CThread::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 kodi::tools::CThread::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 kodi::tools::CTimer.

◆ Sleep()

void kodi::tools::CThread::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

◆ StopThread()

void kodi::tools::CThread::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.

◆ ~CThread()

virtual kodi::tools::CThread::~CThread ( )
inlinevirtual

Class destructor.

Variable Documentation

◆ m_threadStop

std::atomic<bool> kodi::tools::CThread::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