This class provides the required mechanism to manage and control threads.
The main way to use CYIThread is to make a subclass, and implmement the CYIThread::Run() and CYIThread::CleanUp() function. The CYIThread::Run() is executed by the new thread after CYIThread::StartThread() has been called. CYIThread::CleanUp() is called as soon as Run() exits. CYIThread::CleanUp will also be called if the user forcefully terminates the thread by calling CYIThread::Terminate().
#include <thread/YiThread.h>
Classes | |
class | Listener |
Public Types | |
enum | Priority { Priority::Minimum = 0, Priority::Low, Priority::Normal, Priority::High, Priority::Maximum, Priority::Inherited, Priority::Default = Inherited } |
enum | StartType { StartType::NonBlocking = 0, StartType::BlockUntilStarted = 1 } |
Public Member Functions | |
CYIThread (const CYIString &name=CYIString(), Priority priority=Priority::Default, size_t stackSize=DEFAULT_STACK_SIZE) | |
virtual | ~CYIThread () |
bool | StartThread (StartType startType=StartType::BlockUntilStarted) |
void | Terminate () |
void | EnableTermination () |
void | DisableTermination () |
bool | IsTerminationEnabled () |
bool | Join () |
bool | Join (uint32_t timeoutMs) |
bool | IsRunning () const |
CYIThreadHandle | GetThreadId () const |
void | SetName (const CYIString &name) |
const CYIString & | GetName () const |
void | SetPriority (Priority priority) |
Priority | GetPriority () const |
bool | AddThreadListener (Listener *pListener) |
bool | RemoveThreadListener (Listener *pListener) |
bool | SetStackSize (size_t stackSize) |
size_t | GetStackSize () const |
Static Public Member Functions | |
static CYIThreadHandle | GetCurrentThreadId () |
static const CYIThreadHandle & | GetUIThreadId () |
static void | Sleep (uint32_t millis) |
static void | YieldCurrentThread () |
static void | ReportDefaults () |
Static Public Attributes | |
static const size_t | DEFAULT_STACK_SIZE = 128 * 1024 |
Protected Member Functions | |
virtual void | Run ()=0 |
bool | IsTerminationRequested () const |
Friends | |
class | CYIThreadPriv |
|
strong |
|
strong |
CYIThread::CYIThread | ( | const CYIString & | name = CYIString() , |
Priority | priority = Priority::Default , |
||
size_t | stackSize = DEFAULT_STACK_SIZE |
||
) |
|
virtual |
Blocking until the thread terminates.
Calls Join() before cleaning up the thread.
bool CYIThread::AddThreadListener | ( | Listener * | pListener | ) |
Register a CYIThreadListener to monitor this thread.
void CYIThread::DisableTermination | ( | ) |
Manually disables the thread termination feature. Calling Terminate() will have no effect.
void CYIThread::EnableTermination | ( | ) |
Manually enables the thread termination feature. Calling Terminate() will terminate the thread's execution if thread termination is supported on the platform.
Thread termination is enabled by default on platforms which support thread termination. This function will assert on platforms which do not support thread termination.
|
static |
Returns the CYIThreadHandle of the caller's current context.
const CYIString& CYIThread::GetName | ( | ) | const |
Gets the name of the thread
Priority CYIThread::GetPriority | ( | ) | const |
Gets the priority of the thread
size_t CYIThread::GetStackSize | ( | ) | const |
Returns the thread stack size.
CYIThreadHandle CYIThread::GetThreadId | ( | ) | const |
Gets the thread handle of the thread. Will print a warning if the thread is not running.
|
static |
Returns the CYIThreadHandle of You.i Engine's UI thread.
bool CYIThread::IsRunning | ( | ) | const |
Checks if the thread is running.
bool CYIThread::IsTerminationEnabled | ( | ) |
Checks if the termination feature is turned on or off. If this function returns false, it means that Terminate() won't have any effect other than requesting termination.
|
protected |
This function immediately returns true after Terminate() has been invoked.
By default, calling Terminate() aborts the thread execution (if thread termination is supported on the platform). The user can chose to disable thread termination by invoking DisableTermination(), and then poll IsTerminationRequested on regular intervals by the thread's code (Run()) to check if the thread was requested to terminate.
bool CYIThread::Join | ( | ) |
Waits for the thread to terminate.
bool CYIThread::Join | ( | uint32_t | timeoutMs | ) |
Waits for the thread to terminate with a time limit.
bool CYIThread::RemoveThreadListener | ( | Listener * | pListener | ) |
Unregister a CYIThreadListener.
|
static |
Prints a thread report based on default thread settings.
|
protectedpure virtual |
Implemented in CYIEventDispatcherThread.
void CYIThread::SetName | ( | const CYIString & | name | ) |
Changes the name of the thread. Can also be changed while the thread is running. Very useful for debugging.
void CYIThread::SetPriority | ( | Priority | priority | ) |
Changes the priority of the thread. Can also be changed while the thread is running.
bool CYIThread::SetStackSize | ( | size_t | stackSize | ) |
Changes the stack size of the thread.
|
static |
Sleep function. Time unit is in millisecond.
Wraps the YI_SLEEP macro.
bool CYIThread::StartThread | ( | StartType | startType = StartType::BlockUntilStarted | ) |
void CYIThread::Terminate | ( | ) |
Asynchronously aborts the thread's execution if thread termination is enabled. Returns immediately without waiting for the actual termination. The user needs to wait for the thread to terminate, it can be done by either call Join(), or listening for the Terminated signal, or register for the event OnThreadTerminated by implementing the listener class above.
After calling this function, IsTerminationRequested() will return true.
|
static |
Request to yield execution to another thread that is ready to run on the current processor.
|
friend |
|
static |