MetriVis Overview | Download | User Manual | Development
Reference Overview | Design Documentation | Reference Backend | Reference Frontend

metrivis::Thread Class Reference

Interface class for Boost::Thread(s). More...

#include <Thread.h>

Inheritance diagram for metrivis::Thread:

metrivis::HTTPDriver metrivis::RequestDispatcher MyThread metrivis::RawDataDispatcher metrivis::SearchDispatcher metrivis::SQL2JsonDispatcher metrivis::TimeLineDispatcher metrivis::XYPlotDispatcher

Public Member Functions

 Thread ()
 Initializes the Thread.
virtual ~Thread ()
 Initializes the Thread.
virtual int Init ()
 Initializes the Thread. Default implementation sets internal state.
int Start ()
 Starts the Thread.
int Stop ()
 Stops the Thread.
int StopBlocking ()
 Asks to stop the thread and blocks until thread finished.
virtual int Uninit ()
 Initializes the Thread. Default implementation sets internal state.
int RunBlocking ()
 Doesn't create a system thread but executes the main loop manually.
void Wait ()
 Stops the current thread until the other thread finished working.
boost::thread & thread () const
 Interface function to internal thread object.
bool IsRunning () const
 Interface function to internal Thread state.
void set_name (const std::string &name)
 Interface function to thread name.
const std::string & name () const
 Interface function to thread name.

Protected Member Functions

virtual void Run ()=0
 Threading code. Will be run after thread start.

Protected Attributes

volatile bool running_
 True if the thread is running.

Private Types

enum  State { UNUSED = 0, INITIALIZED, RUNNING }
 The internal state representation. More...

Private Member Functions

void InternalRun ()
 Wrapps itself around abstract Thread::Run for state setting.

Private Attributes

State internal_state_
 Representation of internal state.
boost::thread * thread_
 Internal thread reference.
boost::mutex run_mutex_
 Mutex locks running/stopping changes.
std::string name_
 The name of the thread.

Detailed Description

Interface class for Boost::Thread(s).

Each thread object represents a thread in user-space. This class offers a similar interface as Java-Threads. Threads hold internal state informations that help the class to manage the thread-state , it may be: [UNUSED, INITIALIZED, RUNNING].

Every thread needs to be used in the following order:

 Thread t;
 t.Init()
 t.Start();
 t.Stop();
 t.Uninit();
An internal state variable keeps track of the state of the Thread and won't allow any other order than the logical order depicted above. The thread state is saved in Thread::internal_state_.

A thread use in your code like this:

 class MyThread : public Thread {
 public:
   MyThread();

   virtual void Init();

 protected:
   void Run();
 };
Some remarks on the implementation: Don't forget to set the Thread state if you are overloading Thread::Init or Thread::Uninit:
 void MyThread::Init() {
  //This will call the underlaying Thread::Init() function.
  Thread::Init();

  // Do your own stuff here...
 }

Attention:
Thread is an abstract class because of the abstract function Thread::Run and therefore any derived class needs to define this function.

Member Enumeration Documentation

enum metrivis::Thread::State [private]

The internal state representation.

Represents the state a thread may be in. The thread is not running unless the

Thread::internal_state_ == RUNNING 


Constructor & Destructor Documentation

metrivis::Thread::Thread (  ) 

Initializes the Thread.

Constructor for Thread class

Attention:
See header file for overview.

metrivis::Thread::~Thread (  )  [virtual]

Initializes the Thread.

Deconstructor for HTTPDriver class

Attention:
See header file for overview.


Member Function Documentation

int metrivis::Thread::Init (  )  [virtual]

Initializes the Thread. Default implementation sets internal state.

Initializes the Thread.

Initializes the Thread class. The default implementation of this function just sets internal state informations to the initialized state. Call this function always or set the Thread::internal_state_ manually to INITIALIZED.

Attention:
See header file for overview.

Reimplemented in metrivis::HTTPDriver, MyThread, metrivis::RawDataDispatcher, metrivis::SearchDispatcher, metrivis::SQL2JsonDispatcher, metrivis::TimeLineDispatcher, and metrivis::XYPlotDispatcher.

int metrivis::Thread::Start (  ) 

Starts the Thread.

This function starts the thread. It will call Thread::Run, where the thread code is executed.

Attention:
See header file for overview.

int metrivis::Thread::Stop (  ) 

Stops the Thread.

This function stops the thread. It will set the internal state information to

INITIALIZED 
which must be checked in Thread::Run. If Thread::Run is blocking the thread may not be stoppable.

Attention:
See header file for overview.

int metrivis::Thread::StopBlocking (  ) 

Asks to stop the thread and blocks until thread finished.

Stops the Thread, blocks until it finishes.

The same as boost::thread::join(). The current thread of execution blocks until the initial function of the thread of execution represented by *this finishes and all resources are reclaimed.

Attention:
See header file for overview.

int metrivis::Thread::Uninit (  )  [virtual]

Initializes the Thread. Default implementation sets internal state.

Uninitializes the Thread.

Uninitializes the Thread class. The default implementation of this function just sets internal state informations to the initialized state. Call this function always or set the Thread::internal_state_ manually to

UNUSED 
.

Attention:
See header file for overview.

Reimplemented in metrivis::HTTPDriver, MyThread, metrivis::RawDataDispatcher, metrivis::SearchDispatcher, metrivis::SQL2JsonDispatcher, metrivis::TimeLineDispatcher, and metrivis::XYPlotDispatcher.

int metrivis::Thread::RunBlocking (  ) 

Doesn't create a system thread but executes the main loop manually.

Runs the thread code without spawning a thread.

Just starts the main loop manually. Blocks until Thread::Stop is called.

Attention:
See header file for overview.

void metrivis::Thread::Wait (  ) 

Stops the current thread until the other thread finished working.

Stops the Thread, blocks until it finishes.

The funtion won't return until the thread finished it's work.

Attention:
See header file for overview.

boost::thread& metrivis::Thread::thread (  )  const [inline]

Interface function to internal thread object.

Returns:
unalterable reference to the internal thread object.

bool metrivis::Thread::IsRunning (  )  const [inline]

Interface function to internal Thread state.

Returns:
true if the thread is still running.

void metrivis::Thread::set_name ( const std::string &  name  )  [inline]

Interface function to thread name.

Parameters:
name,: String representing the thread name.

const std::string& metrivis::Thread::name (  )  const [inline]

Interface function to thread name.

Returns:
String representing the thread name.

virtual void metrivis::Thread::Run (  )  [protected, pure virtual]

Threading code. Will be run after thread start.

Is executed threaded after call to Thread::Start. The function should contain non blocking functions so that the thread may terminate after Thread::Stop is called. A typical main loop may look like this:

 while( running_) {
  //do some stuff
 }
The Thread::is_running_ variable will be set by Thread::Start rsp. Thread::Stop function.

Implemented in metrivis::HTTPDriver, MyThread, metrivis::RawDataDispatcher, metrivis::RequestDispatcher, metrivis::SearchDispatcher, metrivis::SQL2JsonDispatcher, metrivis::TimeLineDispatcher, and metrivis::XYPlotDispatcher.

void metrivis::Thread::InternalRun (  )  [private]

Wrapps itself around abstract Thread::Run for state setting.

Internal run function for the Thread.

This function will call the virtual and abstract Thread::Run function.

Attention:
See header file for overview.


The documentation for this class was generated from the following files:
Generated on Fri Feb 29 16:47:20 2008 for MetriVis by  doxygen 1.5.3