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

#include <uLib/Thread.h>

Detailed Description

A simple wrapper for CreateThread().

When managing threads, I find it useful to have a thread know it's own handle and id
without external data. Thus a thread can f.ex close it's own handle when it is done.
SimpleThread is a convenient encapsulation of that concept.

Your thread function will always recieve a PSimpleThread on Start().
The Id member is used as the thread loop continuation flag, and Stop() sets it to zero
to indicate it is time to terminate the thread.

Note: SimpleThread is started with CreateThread(), not _beginthreadex(),
so be careful with CRTL functions.

Definition at line 88 of file Thread.h.

Public Data

PVOID UserData
 
HANDLE hThread
 
volatile DWORD Id
 

Public Functions

 SimpleThread ()
 
virtual ~SimpleThread ()
 
bool Start (PTHREAD_START_ROUTINE ThrFunc, DWORD msYield=100)
 
bool Stop (DWORD msWait=3000)
 
void Close ()
 

Constructor & Destructor Documentation

◆ SimpleThread()

uLib::SimpleThread::SimpleThread ( )

Initialize the members.

Definition at line 31 of file Thread.cpp.

◆ ~SimpleThread()

uLib::SimpleThread::~SimpleThread ( )
virtual

Stop the thread, then destroy this.

Definition at line 35 of file Thread.cpp.

Member Function Documentation

◆ Start()

bool uLib::SimpleThread::Start ( PTHREAD_START_ROUTINE  ThrFunc,
DWORD  msYield = 100 
)

Start the ThrFunc thread, and yield a little CPU time to give it a chance to initialize itself.

Definition at line 40 of file Thread.cpp.

◆ Stop()

bool uLib::SimpleThread::Stop ( DWORD  msWait = 3000)

Tell the thread to stop by setting Id to zero, then wait a while for it to exit.
If it doesn't respond within msWait, kill it with TerminateThread().

Definition at line 54 of file Thread.cpp.

◆ Close()

void uLib::SimpleThread::Close ( )

Close the hThread, and clear the Id.

Definition at line 77 of file Thread.cpp.

Member Data Documentation

◆ UserData

PVOID uLib::SimpleThread::UserData

Arbitrary user data (since the usual thread arg is reserved).

Definition at line 90 of file Thread.h.

◆ hThread

HANDLE uLib::SimpleThread::hThread

Thread handle.

Definition at line 91 of file Thread.h.

◆ Id

volatile DWORD uLib::SimpleThread::Id

Thread Id.
When it becomes zero, you should terminate the loop.

Definition at line 92 of file Thread.h.


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