uLib  User mode C/C++ extended API library for Win32 programmers.
uLib::Thread Class Reference

#include <uLib/Thread.h>

Detailed Description

An advanced kill-safe thread, fit as a worker..

Thread is a kill-safe thread with stack unwind, supporting local destructors.
It requires real S.E.H, so it's only available with compilers that support it.
It is based on an article by Jeff Richter in MS Journal, March 1996.

Note: Unlike SimpleThread, Thread is started with _beginthreadex,
making it safer with the multithread CRTL functions.

Definition at line 132 of file Thread.h.

Public Data

PVOID UserData
 
HANDLE hThread
 
volatile DWORD idThread
 

Public Functions

 Thread (PTHREAD_START_ROUTINE pFunc, SIZE_T StackSize=0, DWORD Flags=0, PSECURITY_ATTRIBUTES pSec=NULL)
 
virtual ~Thread ()
 
bool SetThreadProc (PTHREAD_START_ROUTINE pFunc)
 
bool Start ()
 
bool Stop (DWORD msWait, bool mayNuke=false)
 
bool Pause (bool pause)
 
bool Kill ()
 
void DelayDeath (bool enable)
 

Protected Member Functions

virtual DWORD Execute ()
 

Protected Attributes

PTHREAD_START_ROUTINE _ThreadFunc
 
PSECURITY_ATTRIBUTES _SecAttr
 
SIZE_T _StkSize
 
DWORD _Flags
 

Constructor & Destructor Documentation

◆ Thread()

uLib::Thread::Thread ( PTHREAD_START_ROUTINE  pFunc,
SIZE_T  StackSize = 0,
DWORD  Flags = 0,
PSECURITY_ATTRIBUTES  pSec = NULL 
)

Note the absence of a standard thread argument. That is because
Your thread function will always receive a PThread as it's argument.

If you need to pass arbitrary data to your thread function, you can put it in UserData,
or derive a Thread descendant with the added members.

It is not essential to override Execute() just to add some custom data,
as you may simply cast the PThread you get to your descendant type.

Note
When constructing a descendant that overrides Execute(), specify pFunc = NULL.
Parameters
pFuncThread loop function.. See Stop() notes.
StackSizeThread stack size. 0 = use default.
FlagsFlags for _beginthreadex.
pSecSecurity attributes for the thread.

Definition at line 136 of file Thread.cpp.

◆ ~Thread()

uLib::Thread::~Thread ( )
virtual

Stop the thread, then destroy this.

Definition at line 161 of file Thread.cpp.

Member Function Documentation

◆ SetThreadProc()

bool uLib::Thread::SetThreadProc ( PTHREAD_START_ROUTINE  pFunc)

SetThreadProc facilitates re-tasking this instance.
If thread is running, this will fail (of course.. by design).

Definition at line 166 of file Thread.cpp.

◆ Start()

bool uLib::Thread::Start ( )

Start the thread.
Note: Unlike SimpleThread, Thread is started with _beginthreadex.

Definition at line 188 of file Thread.cpp.

◆ Stop()

bool uLib::Thread::Stop ( DWORD  msWait,
bool  mayNuke = false 
)

Stop the thread..

($1) The idThread member is used as a thread loop flag.
Your thread should terminate gracefully when idThread becomes 0.
If it doesn't, it will be peremptorily obliterated!

Definition at line 224 of file Thread.cpp.

◆ Pause()

bool uLib::Thread::Pause ( bool  pause)

Pause or resume the thread.

Definition at line 254 of file Thread.cpp.

◆ Kill()

bool uLib::Thread::Kill ( )

Special feature for the controller:
"Terminate now!!", Force the thread to terminate.

Definition at line 261 of file Thread.cpp.

◆ DelayDeath()

void uLib::Thread::DelayDeath ( bool  enable)

Special feature for the thread:
"I can't die right now!"

Definition at line 312 of file Thread.cpp.

◆ Execute()

virtual DWORD uLib::Thread::Execute ( )
inlineprotectedvirtual

You may override Execute() if you write a descendant Thread class.

By default, Execute just calls _ThreadFunc, for those who prefer the
simpler case of just writing a thread function in the traditional way.

Note: When constructing a descendant that overrides Execute(), specify pFunc = NULL in the constructor call.

Definition at line 197 of file Thread.h.

Member Data Documentation

◆ UserData

PVOID uLib::Thread::UserData

Arbitrary application data.

Definition at line 134 of file Thread.h.

◆ hThread

HANDLE uLib::Thread::hThread

Thread handle.

Definition at line 135 of file Thread.h.

◆ idThread

volatile DWORD uLib::Thread::idThread

Thread Id.($1) See Stop().

Definition at line 136 of file Thread.h.

◆ _ThreadFunc

PTHREAD_START_ROUTINE uLib::Thread::_ThreadFunc
protected

Your thread function (from the c'tor).

Definition at line 199 of file Thread.h.

◆ _SecAttr

PSECURITY_ATTRIBUTES uLib::Thread::_SecAttr
protected

Your security attributes (from the c'tor).

Definition at line 200 of file Thread.h.

◆ _StkSize

SIZE_T uLib::Thread::_StkSize
protected

Your stack size (from the c'tor).

Definition at line 201 of file Thread.h.

◆ _Flags

DWORD uLib::Thread::_Flags
protected

Your CreateThread flags (from the c'tor).

Definition at line 202 of file Thread.h.


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