uLib
User mode C/C++ extended API library for Win32 programmers.
|
#include <uLib/Thread.h>
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.
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 |
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.
pFunc | Thread loop function.. See Stop() notes. |
StackSize | Thread stack size. 0 = use default. |
Flags | Flags for _beginthreadex. |
pSec | Security attributes for the thread. |
Definition at line 136 of file Thread.cpp.
|
virtual |
Stop the thread, then destroy this.
Definition at line 161 of file Thread.cpp.
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.
bool uLib::Thread::Start | ( | ) |
Start the thread.
Note: Unlike SimpleThread, Thread is started with _beginthreadex.
Definition at line 188 of file Thread.cpp.
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.
bool uLib::Thread::Pause | ( | bool | pause | ) |
Pause or resume the thread.
Definition at line 254 of file Thread.cpp.
bool uLib::Thread::Kill | ( | ) |
Special feature for the controller:
"Terminate now!!", Force the thread to terminate.
Definition at line 261 of file Thread.cpp.
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.
|
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.
volatile DWORD uLib::Thread::idThread |
|
protected |
|
protected |
|
protected |
|
protected |