Tanl Linguistic Pipeline |
A thread is an abstract base class that wraps an OS thread, within a thread pool context, i.e., it does all of the grunt work of waiting for tasks and coordinating with the thread pool object to which it belongs. More...
#include <ThreadPool.h>
Classes | |
union | TaskDescr |
Public Member Functions | |
void | Start () |
Causes this thread to begin execution, calling its Run() method. | |
Protected Member Functions | |
Thread (ThreadPool &) | |
Construct a thread object, passing it a pointer to ourselves. | |
virtual | ~Thread () |
Destroy a thread. | |
virtual Thread * | clone (ThreadPool &) const =0 |
Used to create another thread to add to the pool. | |
virtual void | Run (TaskDescr)=0 |
Thread (Thread const &) | |
forbid copy | |
Thread & | operator= (Thread const &) |
forbid assignment | |
Static Protected Member Functions | |
static TRESULT | Main (void *) |
Main function for the OS thread, which invokes Run(). | |
static void | Destroy (void *p) |
Data cleanup handler, called upon thread termination. | |
Protected Attributes | |
pthread_t | thread |
our POSIX thread | |
ThreadPool & | pool |
to which we belong | |
bool | destroying |
within Destroy() | |
Friends | |
class | ThreadPool |
A thread is an abstract base class that wraps an OS thread, within a thread pool context, i.e., it does all of the grunt work of waiting for tasks and coordinating with the thread pool object to which it belongs.
The derived class must override the pure virtual member function Run(). The Run() function gets invoked with an argument taken from the task queue.
IXE::ThreadPool::Thread::Thread | ( | ThreadPool & | p | ) | [protected] |
Construct a thread object, passing it a pointer to ourselves.
Start() must be called for the thread become active.
void IXE::ThreadPool::Thread::Destroy | ( | void * | p | ) | [static, protected] |
Data cleanup handler, called upon thread termination.
This is the clean-up function that gets called upon a thread's death, but only in case the derived class calls ThreadExit() directly.
We must ensure that the thread destructor gets called.
NOTE
This function is declared extern "C" since it is called via the C library function pthread_cleanup_push() and, because it's a C function, it expects C linkage.
p | Pointer to an instance of a ThreadPool::Thread. |
References destroying, and pool.
Referenced by Main().
TRESULT IXE::ThreadPool::Thread::Main | ( | void * | p | ) | [static, protected] |
Main function for the OS thread, which invokes Run().
This is the starting point of execution for a thread.
It waits for a task to appear in its thread pool's task queue and performs the task. After completion, it waits for the next task.
If more threads currently exist than the minimum requested (because more were created as a result of a high request load), then only wait a finite amount of time for a task to appear. If no task appears in that amount of time, then the request load must've lessened so we can destroy this thread.
NOTE
This function is declared extern "C" since it is called via the C library function pthread_create() and, because it's a C function, it expects C linkage.
p | Pointer to an instance of a ThreadPool::Thread. |
References Destroy(), pool, and thread.
Referenced by Start().
void IXE::ThreadPool::Thread::Start | ( | ) |