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

Thread Class Reference

#include <thread.h>

Inheritance diagram for Thread::

myThread List of all members.

Public Methods

 Thread ()
virtual ~Thread ()
int Start (void *arg)

Protected Methods

int Run (void *arg)
virtual void Setup ()
virtual void Execute (void *)
void* Arg () const
void Arg (void *a)
void Mutex (pthread_mutex_t *a)
pthread_mutex_t* Mutex () const
virtual void Lock ()
virtual void Unlock ()

Static Protected Methods

void* EntryPoint (void *)

Private Attributes

pthread_t a_thread
void* Arg_
pthread_mutex_t* p_mutex

Detailed Description

Thread support class
This class must be inherited by any thread class you make
in order to work with the agent subsystem.
Test:
Test threading subsystem and ensure Threads actually work

Mutex sharing

Todo:
Support more complex pthread handling

Definition at line 32 of file thread.h.


Constructor & Destructor Documentation

Thread::Thread ( )
 

Default constructor If you overload this, Make sure your class calls Thread() to setup the mutex.

Definition at line 29 of file thread.cpp.

Referenced by myThread::myThread().

00030 {
00031     p_mutex = NULL;
00032     agent_new(&p_mutex);
00033     {assert(p_mutex != NULL);}
00034     pthread_mutex_init(p_mutex, NULL);
00035 }

Thread::~Thread ( ) [virtual]
 

Default destructor Overload this, but include the following code in your destructor

Definition at line 42 of file thread.cpp.

00043 {
00044     {assert(p_mutex != NULL);}
00045     pthread_mutex_destroy(p_mutex);
00046 }


Member Function Documentation

void Thread::Arg ( void * a ) [inline, protected]
 

Set the current argument (casted to void*)

Parameters:
a   A pointer to what will be passed to Run when your thread is Started

Definition at line 53 of file thread.h.

00053 {Arg_ = a;}

void * Thread::Arg ( ) const [inline, protected]
 

Return the current argument casted to void*

Returns:
A pointer to thread's argument

Definition at line 47 of file thread.h.

Referenced by EntryPoint(), and Start().

00047 {return Arg_;}

void * Thread::EntryPoint ( void * pthis ) [static, protected]
 

Function called from Start Takes a Thread pointer as argument NOTE: Don't EVER CALL THIS. Start will call this

Parameters:
pthis   Thread pointer casted to void *
Returns:
returns Thread pointer after Run was called
See also:
Setup , Execute

Definition at line 90 of file thread.cpp.

00091 {
00092    Thread * pt = (Thread*)pthis;
00093    pt->Run(pt->Arg());
00094    return pt;
00095 }

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

Actual thread code Overload this in your thread, or it wont do anything

See also:
Run , Setup

Reimplemented in myThread.

Definition at line 114 of file thread.cpp.

Referenced by Run().

00115 {
00116         // Your code goes here
00117 }

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

Lock the mutex associated with this thread

See also:
Unlock

Definition at line 123 of file thread.cpp.

00124 {
00125     pthread_mutex_lock(p_mutex);
00126 }

pthread_mutex_t * Thread::Mutex ( ) const [inline, protected]
 

Return the current mutex (for mutex sharing)

Returns:
A pointer to thread's mutex

Definition at line 63 of file thread.h.

00063 {return p_mutex;}

void Thread::Mutex ( pthread_mutex_t * a ) [inline, protected]
 

Set the mutex (for mutex sharing)

Parameters:
a   Pointer to your pthread_mutex_t

Definition at line 58 of file thread.h.

00058 {p_mutex = a;}

int Thread::Run ( void * arg ) [protected]
 

Default entrypoint for threads Calls Setyp and Execute(arg)

Parameters:
arg   Argument to pass to thread
Returns:
returns 0
See also:
Setup , Execute

Definition at line 72 of file thread.cpp.

Referenced by EntryPoint().

00073 {
00074    Setup();
00075    Execute(arg);
00076    return 0;
00077 }

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

Handle the setup of your thread Overload this in your thread, or it wont do anything

See also:
Run , Execute

Reimplemented in myThread.

Definition at line 103 of file thread.cpp.

Referenced by Run().

00104 {
00105         // Do any setup here
00106 }

int Thread::Start ( void * arg )
 

Start the thread running in its own context Pass any argument you want as a void *, but make sure your thread knows how to handle it.

Parameters:
arg   Argument to pass to thread
Returns:
returns the value pthread_create returned
See also:
EntryPoint

Definition at line 56 of file thread.cpp.

Referenced by main().

00057 {
00058    Arg(arg); // store user data
00059    int code = pthread_create(&a_thread, NULL, (void *(*) (void *))
00060         Thread::EntryPoint, (void *) this);
00061    return code;
00062 }

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

Unlock the mutex associated with this thread

See also:
Lock

Definition at line 132 of file thread.cpp.

00133 {
00134     pthread_mutex_unlock(p_mutex);
00135 }


Member Data Documentation

void * Thread::Arg_ [private]
 

Argument storage

Definition at line 70 of file thread.h.

pthread_t Thread::a_thread [private]
 

Definition at line 67 of file thread.h.

pthread_mutex_t * Thread::p_mutex [private]
 

Mutex for locking

Definition at line 72 of file thread.h.


The documentation for this class was generated from the following files:
Generated at Thu May 30 15:12:35 2002 for Freeagent by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001