Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
GrayLib::cThread Class Reference

#include <cThread.h>

Inheritance diagram for GrayLib::cThread:
GrayLib::cThreadBase Gray::cRefBase Gray::cThreadState Gray::CObject Gray::cThreadId IUnknown GrayLib::cThreadChecked GrayLib::cThreadNamed< cUnitTestHttpClient > GrayLib::cThreadNamed< cNetServerThread > GrayLib::cThreadNamed< cJobThreadSeq > GrayLib::cThreadNamed< cUnitTestSecureClient > GrayLib::cThreadNamed< cJobThread > GrayLib::cThreadNamed< cUnitTestOSPipe > GrayLib::cThreadNamed< cUnitTestXProtocolClient > GrayLib::cThreadNamed< cCmdCPULoadThread > GrayLib::cThreadNamed< cUnitTestSSL > GrayLib::cThreadNamed< cLogThread > GrayLib::cThreadNamed< cUnitTestThread > GrayLib::cThreadNamed< cThreadFiber > GrayLib::cThreadNamed< _TYPE >

Public Member Functions

 cThread ()
 
virtual ~cThread ()
 
bool isValidThreadHandle () const noexcept
 
THREADID_t get_HashCode () const noexcept
 
virtual bool isValidCheck () const noexcept
 < memory allocation and structure definitions are valid. More...
 
 STDMETHOD_ (cString, get_Name)() const
 override memory allocation and structure definitions are valid. More...
 
bool isThreadSleeping () const noexcept
 
THREAD_EXITCODE_t get_ExitCodeThread () const
 
int get_ThreadPriority () const
 
bool put_ThreadPriority (int nPriority)
 
bool SuspendThread ()
 
bool ResumeThread ()
 
virtual HRESULT SleepThread (TIMESYSD_t dwWaitMillisec=1, bool bAlertable=false)
 
virtual void WakeThread ()
 
virtual bool RequestStopThread (bool bWillWait=false) noexcept override
 
bool ExitCurrentThread (THREAD_EXITCODE_t nExitCode=THREAD_EXITCODE_OK)
 
bool WaitForThreadExit (TIMESYSD_t nMilliSec=cTimeSys::k_FREQ)
 
THREAD_EXITCODE_t WaitForThreadExitCode (TIMESYSD_t iTimeMSec=cTimeSys::k_FREQ)
 
bool TerminateThread (THREAD_EXITCODE_t iExitCode=THREAD_EXITCODE_ERR, TIMESYSD_t nMilliSec=0)
 
void AttachToCurrentThread ()
 
HRESULT CreateThread (THREAD_FUNC_t pEntryProc, void *pArgs, DWORD dwCreationFlags=0)
 
virtual HRESULT CreateThread (DWORD dwCreationFlags=0)
 
void CloseThread ()
 
- Public Member Functions inherited from GrayLib::cThreadBase
 cThreadBase () noexcept
 
virtual ~cThreadBase ()
 
- Public Member Functions inherited from Gray::CObject
virtual ~CObject ()
 
virtual void AssertValid () const
 < memory allocation and structure definitions are valid. More...
 
virtual void Serialize (cArchive &a)
 
- Public Member Functions inherited from Gray::cThreadId
 cThreadId (THREADID_t nThreadId=k_NULL) noexcept
 
THREADID_t GetThreadId () const noexcept
 
THREADID_t get_HashCode () const noexcept
 
bool isCurrentThread () const noexcept
 
bool isValidId () const noexcept
 
void InitCurrentId () noexcept
 
- Public Member Functions inherited from Gray::cRefBase
 cRefBase (int iRefCount=0) noexcept
 
virtual ~cRefBase ()
 
int get_RefCount () const noexcept
 
HASHCODE_t get_HashCode () const noexcept
 
 STDMETHOD_ (HASHCODE_t, get_HashCodeX)() const noexcept
 
virtual void onFinalRelease ()
 
bool isValidObj () const noexcept
 
 STDMETHOD_ (ULONG, AddRef)(void) override
 
 STDMETHOD_ (ULONG, Release)(void) override
 
STDMETHOD() QueryInterface (const IID &riid, void __RPC_FAR *__RPC_FAR *ppvObject) override
 
void IncRefCount ()
 
void DecRefCount ()
 
bool isStaticConstruct () const noexcept
 
void StaticConstruct ()
 
void StaticDestruct ()
 
bool isDestructing () noexcept
 
void SetDestructing ()
 
- Public Member Functions inherited from Gray::cThreadState
 cThreadState () noexcept
 
bool isThreadRunning () const noexcept
 
bool isThreadStopping () const noexcept
 

Static Public Member Functions

static cThread *GRAYCALL GetCurrentThread ()
 
static bool GRAYCALL IsCurrentThreadStopping ()
 
static void GRAYCALL RequestStopThreadArray (bool bWillWait, cThread **ppThreads, size_t nThreads)
 
static void GRAYCALL TerminateThreadArray (THREAD_EXITCODE_t nExitCode, TIMESYSD_t iTimeMSec, cThread **ppThreads, size_t nThreads)
 
- Static Public Member Functions inherited from Gray::cThreadId
static THREADID_t GetCurrentId () noexcept
 
static bool IsValidId (THREADID_t id) noexcept
 
static bool IsEqualId (THREADID_t a, THREADID_t b) noexcept
 
static void SleepCurrent (TIMESYS_t uMs=cTimeSys::k_FREQ) noexcept
 

Protected Member Functions

virtual bool ThreadTick ()
 
virtual THREAD_EXITCODE_t Run ()
 WorkerThreadProc. More...
 
THREAD_EXITCODE_t RunDirectly ()
 
virtual void onThreadCreate ()
 Notification that the thread has started or stopped. More...
 
virtual void onThreadExit (THREAD_EXITCODE_t nExitCode)
 OnExit. More...
 
void onThreadTerminated (THREAD_EXITCODE_t nExitCode)
 

Static Protected Member Functions

static THREAD_EXITCODE_t _stdcall EntryProc (void *pThisThread)
 _WorkerThreadProc More...
 

Protected Attributes

bool m_bThreadSleeping
 Idle worker threads waiting for something to do. inside Sleep() or WaitForSingleObject() More...
 
bool m_bThreadSuspended
 May be resumed. inside Suspend() -> Resume() More...
 
- Protected Attributes inherited from GrayLib::cThreadBase
THREADHANDLE_t m_hThread
 there may be many handles to the same THREADID_t in _WIN32. I must call CloseThread() on this. More...
 
- Protected Attributes inherited from Gray::cThreadId
THREADID_t m_dwThreadId
 unique thread id. i.e. stack base pointer. (Use the MFC name) More...
 
- Protected Attributes inherited from Gray::cThreadState
bool m_bThreadRunning
 called CreateThread() onThreadCreate(), and inside Run(), until onThreadExit() More...
 
volatile bool m_bThreadStopping
 trying to stop the thread nicely. Do this before TerminateThread() More...
 

Friends

class cThreadMgr
 

Additional Inherited Members

- Static Public Attributes inherited from Gray::cThreadId
static const THREADID_t k_NULL = 0
 Not a valid thread Id. More...
 

Detailed Description

Threads compatible with MFC. Based on CWorkerThread<> Not exactly the same as MFC CWinThread. no message pump.

Note
Use cRefBase so threads can prevent delete (or hold a reference to) themselves until they cleanly stop. Fire and forget.

Constructor & Destructor Documentation

◆ cThread()

GrayLib::cThread::cThread ( )

◆ ~cThread()

GrayLib::cThread::~cThread ( )
virtual

Member Function Documentation

◆ AttachToCurrentThread()

void GrayLib::cThread::AttachToCurrentThread ( )

For attaching the main startup thread to a cThread. early. AKA AttachCurrentThread m_hThread = THREADHANDLE_NULL since we never opened a handle for this.

◆ CloseThread()

void GrayLib::cThread::CloseThread ( )

This thread doesn't exist at all anymore. Final Cleanup. ASSUME: The thread is NOT running. close the handle. Do nothing if AttachToCurrentThread()

◆ CreateThread() [1/2]

◆ CreateThread() [2/2]

HRESULT GrayLib::cThread::CreateThread ( THREAD_FUNC_t  pEntryProc,
void *  pArgs,
DWORD  dwCreationFlags = 0 
)
  • dwCreationFlags = 0 (=start immediate) or CREATE_SUSPENDED; pEntryProc = Use some other pEntryProc routine to exec on the thread. Not Run()

◆ EntryProc()

THREAD_EXITCODE_t _stdcall GrayLib::cThread::EntryProc ( void *  pThisThread)
staticprotected

_WorkerThreadProc

We created a system thread and it calls this.

◆ ExitCurrentThread()

bool GrayLib::cThread::ExitCurrentThread ( THREAD_EXITCODE_t  nExitCode = THREAD_EXITCODE_OK)

Exit the current thread. Must be called on the current cThread. Same as just returning from entrypoint()

◆ get_ExitCodeThread()

THREAD_EXITCODE_t GrayLib::cThread::get_ExitCodeThread ( ) const

Get the value set by the thread when it returned.

Returns
THREAD_EXITCODE_RUNNING = the thread is still running.
THREAD_EXITCODE_ERR = the thread was hard terminated.

◆ get_HashCode()

THREADID_t GrayLib::cThread::get_HashCode ( ) const
inlinenoexcept

Get a unique hash code for the thread. disambiguate.

◆ get_ThreadPriority()

int GrayLib::cThread::get_ThreadPriority ( ) const
Returns
HREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_ABOVE_NORMAL, etc.

TODO get_ThreadPriority linux

◆ GetCurrentThread()

cThread *GRAYCALL GrayLib::cThread::GetCurrentThread ( )
static
Returns
the cThread for the current thread.

◆ IsCurrentThreadStopping()

bool GRAYCALL GrayLib::cThread::IsCurrentThreadStopping ( )
static

◆ isThreadSleeping()

bool GrayLib::cThread::isThreadSleeping ( ) const
inlinenoexcept

◆ isValidCheck()

bool GrayLib::cThread::isValidCheck ( ) const
virtualnoexcept

< memory allocation and structure definitions are valid.

NOT in MFC so use COBJECT_IsValidCheck to call.

Note
This can't be called in constructors and destructors of course !

Reimplemented from Gray::CObject.

◆ isValidThreadHandle()

bool GrayLib::cThread::isValidThreadHandle ( ) const
inlinenoexcept

Handle Is Valid?

Note
Doesn't mean its running. may have stopped.

◆ onThreadCreate()

void GrayLib::cThread::onThreadCreate ( )
protectedvirtual

Notification that the thread has started or stopped.

OnCreate for MFC

Call this when the thread starts. ONLY called ON the thread context itself ! Never by external thread. The id for the thread is different from the thread handle. There may be many handles but only one id ! don't worry about stop/terminating a thread before it starts. true terminate means this will not get called anyhow.

Reimplemented in GrayLib::cThreadChecked.

◆ onThreadExit()

void GrayLib::cThread::onThreadExit ( THREAD_EXITCODE_t  nExitCode)
protectedvirtual

OnExit.

still can still use m_hThread to get the threads return value!

Note
m_hThread is still valid.

Reimplemented in GrayLib::cThreadFiber, and GrayLib::cJobFiber.

◆ onThreadTerminated()

void GrayLib::cThread::onThreadTerminated ( THREAD_EXITCODE_t  nExitCode)
protected

This thread was hard terminated. or crashed ? Clear flags as they don't apply anymore. nExitCode = THREAD_EXITCODE_ERR

◆ put_ThreadPriority()

bool GrayLib::cThread::put_ThreadPriority ( int  nPriority)

The handle must have the THREAD_SET_INFORMATION or THREAD_SET_LIMITED_INFORMATION rights

  • HREAD_PRIORITY_HIGHEST, THREAD_PRIORITY_ABOVE_NORMAL, etc.

◆ RequestStopThread()

bool GrayLib::cThread::RequestStopThread ( bool  bWillWait = false)
overridevirtualnoexcept

Attempt to stop the thread nicely. (polite stop) Signaling myself to stop is legal. isCurrentThread()

Returns
true = already stopped. false = not yet stopped. Must wait. (or hard terminate)

Reimplemented from Gray::cThreadState.

◆ RequestStopThreadArray()

void GRAYCALL GrayLib::cThread::RequestStopThreadArray ( bool  bWillWait,
cThread **  ppThreads,
size_t  nThreads 
)
static

Signal them to stop.

◆ ResumeThread()

bool GrayLib::cThread::ResumeThread ( )

◆ Run()

THREAD_EXITCODE_t GrayLib::cThread::Run ( )
protectedvirtual

WorkerThreadProc.

thread should override this for body.

implementation should virtual override this (or ThreadTick) for body.

Reimplemented in GrayLib::cJobThreadSeq.

◆ RunDirectly()

THREAD_EXITCODE_t GrayLib::cThread::RunDirectly ( )
protected

Run cThread in the current system thread.

◆ SleepThread()

HRESULT GrayLib::cThread::SleepThread ( TIMESYSD_t  dwWaitMillisec = 1,
bool  bAlertable = false 
)
virtual

Make the current thread sleep. Just wrap the _WIN32 call. virtual so it may act different for cThreadFiber

Reimplemented in GrayLib::cThreadFiber.

◆ STDMETHOD_()

GrayLib::cThread::STDMETHOD_ ( cString  ,
get_Name   
) const

override memory allocation and structure definitions are valid.

What task is this ?

◆ SuspendThread()

bool GrayLib::cThread::SuspendThread ( )

Just prevent this thread from running. Opposite of ResumeThread()

◆ TerminateThread()

bool GrayLib::cThread::TerminateThread ( THREAD_EXITCODE_t  iExitCode = THREAD_EXITCODE_ERR,
TIMESYSD_t  nMilliSec = 0 
)

Terminate the thread. Similar to join(). Don't destroy the handle. Do that in CloseThread() in case we want to check return value until then.

  • nExitCode = The error code to be used if this is a forced thread exit. THREAD_EXITCODE_ERR iTimeMSec = time to wait for thread to close nicely. 0 = hard terminate immediately. Don't bother to try to close nicely.
    Note
    This is DANGEROUS. if the thread is killed while it has a cThreadLock. that lock is now dead! will cause deadlocks.
    Returns
    true = stopped. done

◆ TerminateThreadArray()

void GRAYCALL GrayLib::cThread::TerminateThreadArray ( THREAD_EXITCODE_t  nExitCode,
TIMESYSD_t  iTimeMSec,
cThread **  ppThreads,
size_t  nThreads 
)
static

Terminate (Try Nicely first) an array of worker threads in parallel.

◆ ThreadTick()

virtual bool GrayLib::cThread::ThreadTick ( )
inlineprotectedvirtual

override this to do a chunk of work then return. (the caller will deal with sleep). Called periodically inside default implementation of Run() This thread declares itself alive.

Returns
true = thread should keep running. false = thread should exit.

Reimplemented in GrayLib::cThreadFiber, GrayLib::cThreadChecked, GrayLib::cLogThread, GrayLib::cJobThread, GrayLib::cJobFiber, and GrayLib::cNetServerThread.

◆ WaitForThreadExit()

bool GrayLib::cThread::WaitForThreadExit ( TIMESYSD_t  nMilliSec = cTimeSys::k_FREQ)

wait for this thread to close on it's own. Nicely might assume m_bThreadStopping but not necessary.

  • iTimeMSec = time to wait for it to close.
    Returns
    true = stopped. done

◆ WaitForThreadExitCode()

THREAD_EXITCODE_t GrayLib::cThread::WaitForThreadExitCode ( TIMESYSD_t  iTimeMSec = cTimeSys::k_FREQ)

Close and Wait for the thread to exit and get its exit code.

◆ WakeThread()

void GrayLib::cThread::WakeThread ( )
virtual

Friends And Related Function Documentation

◆ cThreadMgr

friend class cThreadMgr
friend

Member Data Documentation

◆ m_bThreadSleeping

bool GrayLib::cThread::m_bThreadSleeping
protected

Idle worker threads waiting for something to do. inside Sleep() or WaitForSingleObject()

◆ m_bThreadSuspended

bool GrayLib::cThread::m_bThreadSuspended
protected

May be resumed. inside Suspend() -> Resume()


The documentation for this class was generated from the following files: