The Micro C++ Library
micxx::Thread Class Reference

A Thread represents a thread of execution within a process that could be run synchronously or started asynchronously and joined later to regain synchronization. More...

#include <micxx.hxx>

Inherits micxx::Mutex.

List of all members.

Public Member Functions

 Thread (const char *name)
 Create a new Thread with the specified name aname.
 Thread (const std::string &name="micxx::Thread")
 Create a new Thread with the specified name aname.
virtual ~Thread ()
 Delete all resources that are bound to the Thread.
virtual ThreadId getId () const
 The id of this Thread.
virtual const std::string & getName () const
 The name of this Thread.
virtual const ExceptiongetException () const
 The stored Exception.
virtual bool isRunning () const
 Only true, if this Thread is executing asynchronously.
virtual void run ()
 The method that will be executed from the Thread after the Thread has been started.
virtual void join ()
 Wait for this Thread to complete.
virtual void setName (const std::string &name)
 Set the threads name to name.
virtual void setException (const Exception *exception)
 Store a copy of Exception exception.
virtual void setRunnable (traits::Runnable &runnable)
 Set the runnable that will be used in the thread default run() implementation.
virtual void start ()
 Start this Thread and executes the Threads run() method.

Static Public Member Functions

static ThreadId getCurrentId ()
 The id of the current Thread.
static void sleep (long millis)
 Suspend the current thread for millis milli seconds.

Protected Member Functions

virtual void stopped ()
 Callback from the thread just before the thread exists its execution path.

Protected Attributes

traits::Runnablerunnable

Friends

void * _threadProc (void *lpParam)

Detailed Description

A Thread represents a thread of execution within a process that could be run synchronously or started asynchronously and joined later to regain synchronization.

Example:

 #include <micxx.hxx>

 class MyThread : public micxx::Thread
 {
 public:
     void run()
     {
         for (int n = 0; n < 10; ++n)
         {
             micxx::Synchronized synchronized(micxx::System::getMutex());
             std::cout << n << std::endl;
             Thread::sleep(1000);
         }
     }
 };

 int main(int argc, const char * args[])
 {
     micxx::System system(argc, args);
     try
     {
         MyThread myThread;
         myThread.start();
         myThread.join();
     }
     catch (const Exception & exception)
     {
         cerr << exception << endl;
         return EXIT_FAILURE;
     }
     return EXIT_SUCCESS;
 }
Author:
Norbert Klose
Date:
December, 2006

Constructor & Destructor Documentation

micxx::Thread::Thread ( const char *  name)

Create a new Thread with the specified name aname.

micxx::Thread::Thread ( const std::string &  name = "micxx::Thread")

Create a new Thread with the specified name aname.

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

Delete all resources that are bound to the Thread.


Member Function Documentation

static ThreadId micxx::Thread::getCurrentId ( ) [static]

The id of the current Thread.

virtual const Exception* micxx::Thread::getException ( ) const [inline, virtual]

The stored Exception.

    {
        return exception;
    }
virtual ThreadId micxx::Thread::getId ( ) const [inline, virtual]

The id of this Thread.

    {
#ifdef MICXX_WITH_PTHREAD
        return thread;
#elif _WIN32
        return id;
#endif
    }
virtual const std::string& micxx::Thread::getName ( ) const [inline, virtual]

The name of this Thread.

    {
        return name;
    }
virtual bool micxx::Thread::isRunning ( ) const [inline, virtual]

Only true, if this Thread is executing asynchronously.

A thread is executing asynchronously after start() has been called and as long as run() is executing.

    {
        return running;
    }
virtual void micxx::Thread::join ( ) [virtual]

Wait for this Thread to complete.

Note:
When run() has thrown an exception during its execution from the Thread, or if the application has stored an exception with a call to setException(const Exception*), this exception is thrown here.
virtual void micxx::Thread::run ( ) [virtual]

The method that will be executed from the Thread after the Thread has been started.

This method just calls run on a runnable, if that has been set before. If the run() method throws an exception, this exception will be stored and thrown by join().

Note:
It is illegal to delete this instance from within run(). Use the callback stopped() for this purpose.
See also:
stopped(),
setRunnable()
virtual void micxx::Thread::setException ( const Exception exception) [virtual]

Store a copy of Exception exception.

Parameters:
exceptionIf not 0, a copy of exception will be stored, otherwise an already stored Exception is deleted and set to 0.
virtual void micxx::Thread::setName ( const std::string &  name) [virtual]

Set the threads name to name.

virtual void micxx::Thread::setRunnable ( traits::Runnable runnable) [virtual]

Set the runnable that will be used in the thread default run() implementation.

static void micxx::Thread::sleep ( long  millis) [static]

Suspend the current thread for millis milli seconds.

virtual void micxx::Thread::start ( ) [virtual]

Start this Thread and executes the Threads run() method.

virtual void micxx::Thread::stopped ( ) [protected, virtual]

Callback from the thread just before the thread exists its execution path.

Note:
It is valid to delete this instance from within stopped().

Friends And Related Function Documentation

void* _threadProc ( void *  lpParam) [friend]

Member Data Documentation


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