Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members File Members Related Pages

# wkgl::Thread Class Reference

This class allows you to perform multithreading. More...

`#include <Thread.h>`

Inheritance diagram for wkgl::Thread:

List of all members.

## Public Methods
| Thread (RUNNABLE r)
| Creates a new thread.
| Thread (RUNNABLE r, char *name)
| Creates a new thread, with the given name.
| ~Thread (void)
virtual BOOL | start ()
| Executes the RUNNABLE function in a seperate thread.
virtual BOOL | start (void *param)
| Executes the RUNNABLE function in a seperate thread.
virtual BOOL | stop ()
| Terminates the thread.
virtual BOOL | suspend ()
| Suspends the thread.
virtual BOOL | resume ()
| Resumes the thread after it has been suspended.
virtual SFLAG | getState ()
| Returns the current state of the thread.
virtual char * | getName ()
| Returns the name of the thread.
virtual BOOL | waitForCompletion (DWORD mill)
| Stops the execution of the caller unitl the thread terminates.
virtual BOOL | waitForCompletion ()
| Stops the execution of the caller until the thread terminates.
## Static Public Methods
void | sleep (ULONG mill)
| Causes the calling thread to stop execution for the given time.
Thread * | getCurrentThread ()
| Returns the thread of the caller.
void | wait (DWORD mill)
| Causes the calling thread to stop executing until it is notifyed.
void | wait ()
| Causes the calling thread to stop executing until it is notifyed.
void | notify ()
| Notify a single waiting thread.
void | notifyAll ()
| Notifies all waiting threads.
void | yield ()
| Attempts to yeild the calling threads time slice to the next thread.
## Protected Methods
| Thread ()
## Protected Attributes
RUNNABLE | r
| The function to run.
SFLAG | state
| The current state of the thread.
DWORD | thread_id
| The windows id of the thread.
HANDLE | thandle
| The windows handle to the thread.
char * | name
| The name of this thread.
void * | param
| The parameter to pass to r;.
## Static Protected Attributes
HANDLE | notify_event
| This event is used in the wait/notify/notifyAll scheme.
UINT | nwaiting = 0
| Counts the number of waiting threads.
## Friends
DWORD WINAPI | ThreadProc (LPVOID lpParam)
| Used by win32 api.

## Detailed Description

This class allows you to perform multithreading.

Author:
  Micheal Nooner.

This class interacts with the win32 api in order to implement multithreading. To create a thread you must first define a function that takes no parameters and has a void return type, e.g. void XXX(). When you construct the thread object you simply pass a pointer to this function, e.g. Thread t1( XXX ). This creates the thread. To execute the thread call the start method. The start method will return and then the function you passed in will be executed. A common problem is that Windows schedules the execution of threads based on time slices. So the main thread may exit before all your other threads are finished. This is where the waitForCompletion method comes in. It halts the calling thread until the thread object is finished executing. You should be careful when using this method because it is possible to create a program or system wide deadlock.

## Constructor & Destructor Documentation

Thread::Thread | ( |  | ) | `[protected]`

Thread::Thread | ( | RUNNABLE | r | ) |

  | Creates a new thread. Parameters:
  r | The function to run

Thread::Thread | ( | RUNNABLE | r,
|  | char * | name
| ) |

  | Creates a new thread, with the given name. Parameters:
  r | The function to run.
  name | the name of this thread.

Thread::~Thread | ( | void |  | ) |

## Member Function Documentation

Thread * Thread::getCurrentThread | ( |  | ) | `[static]`

| Returns the thread of the caller. So, Thread::getCurrentThread() != Thread::getCurrentThread(). Returns: The callers thread.

char * Thread::getName | ( |  | ) | `[virtual]`

| Returns the name of the thread. Returns: The name of the thread, or NULL if the name is not set.

SFLAG Thread::getState | ( |  | ) | `[virtual]`

| Returns the current state of the thread. Returns: Either THREAD_STOPPED, THREAD_RUNNING, or THREAD_SUSPENDED. Reimplemented in wkgl::ThreadGroup.

void Thread::notify | ( |  | ) | `[static]`

| Notify a single waiting thread.

void Thread::notifyAll | ( |  | ) | `[static]`

| Notifies all waiting threads.

BOOL Thread::resume | ( |  | ) | `[virtual]`

| Resumes the thread after it has been suspended. Returns: TRUE if the thread was successfully resumed after being suspended. Reimplemented in wkgl::ThreadGroup.

void Thread::sleep | ( | ULONG | mill | ) | `[static]`

  | Causes the calling thread to stop execution for the given time. Parameters:
  mill | The amount of time in milliseconds that the thread should be suspended.

BOOL Thread::start | ( | void * | param | ) | `[virtual]`

  | Executes the RUNNABLE function in a seperate thread. Parameters:
  param | The parameter to pass to the runnable function.
Returns: TRUE if the thread was successfully started.

BOOL Thread::start | ( |  | ) | `[virtual]`

| Executes the RUNNABLE function in a seperate thread. Returns: TRUE if the thread was successfully started. Reimplemented in wkgl::ThreadGroup.

BOOL Thread::stop | ( |  | ) | `[virtual]`

| Terminates the thread. Returns: TRUE if the thread was successfully terminated. Reimplemented in wkgl::ThreadGroup.

BOOL Thread::suspend | ( |  | ) | `[virtual]`

| Suspends the thread. Returns: TRUE if the thread was successfully paused. Reimplemented in wkgl::ThreadGroup.

void Thread::wait | ( |  | ) | `[static]`

| Causes the calling thread to stop executing until it is notifyed.

void Thread::wait | ( | DWORD | mill | ) | `[static]`

  | Causes the calling thread to stop executing until it is notifyed. Parameters:
  mill | The number of milliseconds to wait.

BOOL Thread::waitForCompletion | ( |  | ) | `[virtual]`

| Stops the execution of the caller until the thread terminates. Returns: TRUE if the thread has finished executing, FALSE if some error has occured. Reimplemented in wkgl::ThreadGroup.

BOOL Thread::waitForCompletion | ( | DWORD | mill | ) | `[virtual]`

  | Stops the execution of the caller unitl the thread terminates. Parameters:
  mill | The number of milliseconds the calling thread should wait for the target thread to exit.
Returns: TRUE if the thread has finished executing, FALSE if the time has elapsed or some error has occured. Reimplemented in wkgl::ThreadGroup.

void Thread::yield | ( |  | ) | `[static]`

| Attempts to yeild the calling threads time slice to the next thread.

## Friends And Related Function Documentation

DWORD WINAPI ThreadProc | ( | LPVOID | lpParam | ) | `[friend]`

| Used by win32 api.

## Member Data Documentation

char* wkgl::Thread::name`[protected]`

| The name of this thread.

HANDLE Thread::notify_event`[static, protected]`

| Initial value:
     CreateEvent( NULL, TRUE, FALSE,
                                               "MNThreadEvent" )
This event is used in the wait/notify/notifyAll scheme.

UINT Thread::nwaiting = 0`[static, protected]`

| Counts the number of waiting threads.

void* wkgl::Thread::param`[protected]`

| The parameter to pass to r;.

RUNNABLE wkgl::Thread::r`[protected]`

| The function to run.

SFLAG wkgl::Thread::state`[protected]`

| The current state of the thread.

HANDLE wkgl::Thread::thandle`[protected]`

| The windows handle to the thread.

DWORD wkgl::Thread::thread_id`[protected]`

| The windows id of the thread.

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

- Thread.h
- Thread.cpp