cxxtools::AttachedThread Class Reference

Platform independent joinable thread. More...

#include <cxxtools/thread.h>

Inheritance diagram for cxxtools::AttachedThread:
cxxtools::Thread cxxtools::NonCopyable

Public Member Functions

 AttachedThread (const Callable< void > &cb)
 Constructs a thread with a thread entry.
 ~AttachedThread ()
 Joins the thread, if not already joined.
void join ()
 Wait explicitly for the thread to terminate.
void terminate ()
 Terminates the thread.
- Public Member Functions inherited from cxxtools::Thread
virtual ~Thread ()
 Destructor.
State state () const
 Returns the current state of the thread.
void start ()
 Starts the thread.
void create ()

Additional Inherited Members

- Public Types inherited from cxxtools::Thread
enum  State { Ready = 0, Running = 1, Finished = 2 }
 Status of a thread. More...
- Static Public Member Functions inherited from cxxtools::Thread
static void exit ()
 Exits athread.
static void yield ()
 Yield CPU time.
static void sleep (unsigned int ms)
 Sleep for some time.
- Protected Member Functions inherited from cxxtools::Thread
 Thread ()
 Default Constructor.
 Thread (const Callable< void > &cb)
 Constructs a thread with a thread entry.
void init (const Callable< void > &cb)
 Initialize with a thread entry.
void detach ()
 Detaches the thread.
bool joinNoThrow ()
 Joins the thread.

Detailed Description

Platform independent joinable thread.

AttachedThreads are threads, which are managed by the creator, and are normally created on the stack. The creator must wait, until the thread terminates either explicitly by calling join() or implicitly by the destructor. The life-time of the callable object must exceed the life-time of the thread. Mind the order of destruction if the AttachedThread is a member variable of a class.

Example:

struct Operation
{
void run()
{
// implement, whatever needs to be done in parallel
}
};
int main()
{
Operation op;
cxxtools::Thread thread( cxxtools::callable(op, &Operation::run) );
thread.start();
// the thread runs and we can do something else in parallel
doMoreWork();
// the thread's destructor waits for the thread to join
// the op object outlives the thread object
return 0;
}

Constructor & Destructor Documentation

cxxtools::AttachedThread::AttachedThread ( const Callable< void > &  cb)
inlineexplicit

Constructs a thread with a thread entry.

Constructs a thread object to execute the Callable cb. The Thread is not started on construction, but when start() is called.

cxxtools::AttachedThread::~AttachedThread ( )
inline

Joins the thread, if not already joined.

Member Function Documentation

void cxxtools::AttachedThread::join ( )
inline

Wait explicitly for the thread to terminate.

Join() is called automatically in the destructor if not already called. Throws SystemError on failure

Reimplemented from cxxtools::Thread.

void cxxtools::AttachedThread::terminate ( )
inline

Terminates the thread.

Forces the thread to terminate is dangerous and discouraged.

Reimplemented from cxxtools::Thread.


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