Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions

CxUtils::Thread Class Reference

Cross-platform class for creating threads for functions or adding thread capabilities to a class using inheritence. More...

#include <thread.h>

List of all members.

Classes

class  Manager
 Thread Manager class which can be used to keep track of multiple running threads. More...

Public Types

typedef pthread_t ID

Public Member Functions

 Thread ()
 Constructor.
 Thread (const std::string &name)
 Constructor with optional name for VC++ debugger.
virtual ~Thread ()
 Destructor.
int CreateThread ()
 Creates the thread.
int CreateThread (void(*func)(void *), void *args)
 Creates a thread based on a function pointer.
int StopThread (const int ms=500)
 Stop a thread, but wait for it to exit.
int SetThreadPriority (const int priority)
 Call after creating the thread.
int KillThread ()
 Kills the thread forcefully.
bool QuitThreadFlag () const
 If true thread should exit.
bool IsThreadActive () const
 Is the thread still active?
void SetThreadName (const std::string &name)
 Sets the name of the thread in the VC++ debugger.
std::string GetThreadName () const
 Get the name of the thread.
Thread::ID GetThreadID () const
 Gets the thread ID.

Static Public Member Functions

static Thread::ID GetCurrentThreadID ()
 Gets the ID of the current operating thread.

Protected Member Functions

virtual void Execute ()
 The actual thread function.

Detailed Description

Cross-platform class for creating threads for functions or adding thread capabilities to a class using inheritence.

Thread is used to create a threaded function within a class on both Windows and Linux based systems. This is extremely useful and simple to use and makes it easy to have threading on multiple platforms.

To make use of the Thread class just use inheritance and implement a virtual function. The Thread function is where you place code to be performed in the spawned thread. Functions for checking the status of thread and killing it exist.

Definition at line 84 of file thread.h.


Member Typedef Documentation

typedef pthread_t CxUtils::Thread::ID

Definition at line 90 of file thread.h.


Constructor & Destructor Documentation

Thread::Thread (  )

Constructor.

Constructor, sets defaults.

Definition at line 286 of file thread.cpp.

Thread::Thread ( const std::string &  name )

Constructor with optional name for VC++ debugger.

Creates the thread object and then sets the name.

Parameters:
nameName of the thread to display in the VC++ debugger

Definition at line 302 of file thread.cpp.

Thread::~Thread (  ) [virtual]

Destructor.

Destructor, terminates any running threads by force.

Definition at line 374 of file thread.cpp.


Member Function Documentation

int Thread::CreateThread (  )

Creates the thread.

Creates the thread that calls the Thread::Thread function implemented.

Returns 1 if successful, 0 on failure.

Definition at line 400 of file thread.cpp.

int Thread::CreateThread ( void(*)(void *)  func,
void *  args 
)

Creates a thread based on a function pointer.

Creates a thread based on the function and it's arguments.

Uses either windows or pthread function calls to spawn a thread that uses the function passed.

Parameters:
funcFunction pointer for thread operation.
argsArguments for the thread.

Returns true if successful, false on failure.

Definition at line 455 of file thread.cpp.

void Thread::Execute (  ) [protected, virtual]

The actual thread function.

The main execution function called within the thread.

This virtual function is what gets called within the thread created by the createThread function. This is an abstract function, meaning if you inherit Thread, you must implement this function.

Definition at line 691 of file thread.cpp.

Thread::ID Thread::GetCurrentThreadID (  ) [static]

Gets the ID of the current operating thread.

Gets the ID of the current calling thread.

Returns:
Gets the ID of the current calling thread.

Definition at line 341 of file thread.cpp.

Thread::ID Thread::GetThreadID (  ) const

Gets the thread ID.

Returns the identifier of the thread.

Definition at line 605 of file thread.cpp.

std::string Thread::GetThreadName (  ) const

Get the name of the thread.

Returns:
Thread name.

Definition at line 313 of file thread.cpp.

bool Thread::IsThreadActive (  ) const

Is the thread still active?

Returns true if a thread is active, false otherwise.

Definition at line 533 of file thread.cpp.

int Thread::KillThread (  )

Kills the thread forcefully.

Kills the thread forefully.

Returns:
1 if killed, otherwise 0.

Definition at line 546 of file thread.cpp.

bool Thread::QuitThreadFlag (  ) const

If true thread should exit.

Returns:
True if the the thread has been told to quit, otherwise false.

Definition at line 679 of file thread.cpp.

void Thread::SetThreadName ( const std::string &  name )

Sets the name of the thread in the VC++ debugger.

Sets the name of the thread in the VC++ debugger window.

Parameters:
nameName of the thread (9 char limitaiton in VC++ 6)

Definition at line 322 of file thread.cpp.

int Thread::SetThreadPriority ( const int  priority )

Call after creating the thread.

Sets the priority of the thread.

Parameters:
priorityValue ranges form -50, 50, with 0 the default.
Returns:
1 if successfull, CX_FALSE otherwise

Definition at line 620 of file thread.cpp.

int Thread::StopThread ( const int  ms = 500 )

Stop a thread, but wait for it to exit.

If the thread is in a looping state, this function will stop it. If the thread takes longer than ms milliseconds to exit, then the thread is killed.

Parameters:
msHow long to wait for the thread to quit in milliseconds.
Returns:
True if thread stopped.

Definition at line 479 of file thread.cpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines