Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes

base::Thread Class Reference

A thread class. More...

#include <libppbase/base/Thread.h>

Inheritance diagram for base::Thread:
base::WorkerThread

List of all members.

Public Member Functions

 Thread (ThreadList *list=NULL)
 Constructor.
virtual ~Thread ()
 Destructor.
void cancel (bool immediate=false)
 Cancels the thread.
bool cleanup ()
 Cleanup when the thread ends (internal use, don't call it).
unsigned long getCpuMilliSecondsBase () const
 Returns the base milliseconds value that was last set with setCpuMilliSecondsBase().
ThreadListgetList ()
 Returns the thread list that this thread is a member of.
int getPid () const
 Returns the process id.
bool isCancelled () const
 Check if the thread has been cancelled.
bool isRunning () const
 Check if the thread is running.
void launch ()
 Starts the thread (internal use, don't call it).
void reconfig ()
 Sets reconfigure request.
virtual void run ()=0
 The thread's main method, must be overridden by subclass.
void setCpuMilliSecondsBase ()
 Sets the current milliseconds that this thread spent on the CPU as base value.
void setPriority (int priority)
 Sets the thread priority.
void setPriorityBoost (bool enabled)
 Enables the system to temporarily boost the priority of this thread (Windows only).
void start (bool detached=false)
 Starts the thread.
bool wait (unsigned timeout=0)
 Waits for the thread to terminate.

Static Public Member Functions

static unsigned long getCpuMilliSeconds ()
 Returns the number of milliseconds that the current thread has spent on the CPU.
static void sleep (unsigned msecs)
 Sleeps for some milliseconds.

Protected Member Functions

 DISALLOW_COPY_CONSTRUCTOR_AND_ASSIGNMENT (Thread)
 threads cannot be duplicated

Protected Attributes

bool _cancelled
 flag: thread has been cancelled
ThreadList_list
 thread list
unsigned long _msbase
 base milliseconds for measuring cpu usage
int _pid
 the thread's process id
bool _reconfig
 flag: thread should reconfigure itself
bool _running
 flag: thread is running
pthread_t _thread
 a pthread thread variable

Detailed Description

A thread class.

You can create both detached and joinable threads with this class. To create a joinable thread, pass a NULL pointer to the thread list parameter of the constructor. To create a detached thread, pass a valid thread list object to the constuctor. Detached threads remove themselves from the thread list, delete the Thread object (and thereby all resources associated with this object) automatically. In the case of a joinable thread, a different thread has to call the wait() method of the joinable thread's Thread object, thereby waiting for the thread to terminate. If the thread has terminated, the caller of the wait() method has to delete the Thread object thereby freeing all resources associated with this object.

Author:
Christoph Schwarz
Version:
$Id: Thread.h 21 2010-09-05 04:18:17Z cschwarz1 $

Member Function Documentation

void base::Thread::cancel ( bool  immediate = false  ) 

Cancels the thread.

This function sets the _cancelled member to true, allowing the thread to react accordingly. However, if immediate is true, the function also calls an OS-specific function that may terminate the thread earlier (POSIX: pthread_cancel, WIN32: TerminateThread) This "hard" termination might be dangerous, as not all system resources owned by the thread (e.g. mutexes) may be freed, leaving the process in an undefined state.

Parameters:
immediate do a "hard" termination of the thread
bool base::Thread::cleanup (  ) 

Cleanup when the thread ends (internal use, don't call it).

Returns:
true if the thread should be deleted
static unsigned long base::Thread::getCpuMilliSeconds (  )  [static]

Returns the number of milliseconds that the current thread has spent on the CPU.

Returns:
the number of milliseconds
unsigned long base::Thread::getCpuMilliSecondsBase (  )  const

Returns the base milliseconds value that was last set with setCpuMilliSecondsBase().

Returns:
the base value
ThreadList* base::Thread::getList (  ) 

Returns the thread list that this thread is a member of.

Returns:
NULL if the thread is not member of a thread list
int base::Thread::getPid (  )  const

Returns the process id.

Returns:
the process id
bool base::Thread::isCancelled (  )  const

Check if the thread has been cancelled.

Returns:
true if the thread has been cancelled

Referenced by base::WorkPile< Socket * >::dequeue().

bool base::Thread::isRunning (  )  const

Check if the thread is running.

Returns:
true if the thread is running
virtual void base::Thread::run (  )  [pure virtual]

The thread's main method, must be overridden by subclass.

The routine should stop when the _cancelled member variable is set. It should reconfigure itself when the member variable _reconfig is set.

void base::Thread::setCpuMilliSecondsBase (  ) 

Sets the current milliseconds that this thread spent on the CPU as base value.

The base value can later be retrieved with getCpuMilliSecondsBase() to calculate a delta value between the current CPU millisecond value and the old one. This allows measuring how much CPU time the thread has spent with executing a certain task.

void base::Thread::setPriority ( int  priority  ) 

Sets the thread priority.

This function is highly system-dependent. Under windows it calls the SetThreadPriority function, under UNIX the setpriority(2) function.

Parameters:
priority the new priority
void base::Thread::setPriorityBoost ( bool  enabled  ) 

Enables the system to temporarily boost the priority of this thread (Windows only).

Parameters:
enabled true to enable boost, false to disable
void base::Thread::start ( bool  detached = false  ) 

Starts the thread.

Parameters:
detached flag: create a detached thread
bool base::Thread::wait ( unsigned  timeout = 0  ) 

Waits for the thread to terminate.

Parameters:
timeout timeout in seconds, 0 means infinite (works only on Windows, Linux always uses infinite timeout)
Returns:
true if the thread exited, false if a timeout occurred

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