The Micro C++ Library
|
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.
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 Exception * | getException () 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::Runnable * | runnable |
Friends | |
void * | _threadProc (void *lpParam) |
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; }
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.
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] |
virtual void micxx::Thread::join | ( | ) | [virtual] |
Wait for this Thread to complete.
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().
virtual void micxx::Thread::setException | ( | const Exception * | exception | ) | [virtual] |
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] |
virtual void micxx::Thread::stopped | ( | ) | [protected, virtual] |
Callback from the thread just before the thread exists its execution path.
void* _threadProc | ( | void * | lpParam | ) | [friend] |
traits::Runnable* micxx::Thread::runnable [protected] |