#include <archon/util/thread.H>
Inheritance diagram for Archon::Utilities::Thread:
Public Member Functions | |
void | wait () throw (NotStartedException, UnexpectedException) |
Wait for this thread to stop. | |
void | terminate () |
Issue a termination request for this thread. | |
Static Public Member Functions | |
template<typename T> | |
static Ref< Thread > | run (Ref< T >, void(T::*)(), bool start=true) |
Let a new thread invoke a method of your choise on a reference counted object of your choise. | |
template<typename T, typename U> | |
static Ref< Thread > | run (Ref< T >, void(T::*)(U), U, bool start=true) |
Like Thread::run(Ref<T>, void (T::*)()) except that it invokes a method taking one argument, and you must specify that extra argument when calling this method. | |
template<typename T> | |
static Ref< Thread > | run (void(*)(T), T, bool start=true) |
Let a new thread invoke a static 1-argument-function of your choise. | |
static Ref< Thread > | run (void(*)(), bool start=true) |
Let a new thread invoke a static void-function of your choise. | |
static void | start (Ref< Thread >) throw (AlreadyStartedException) |
Let a new thread invoke the virtual main method of the specified Thread object. | |
static Ref< Thread > | self () |
Return a reference to the Thread object associated with the thread that calls this method. | |
static void | sleep (const Time &period) throw (UnexpectedException) |
Make the calling thread sleep for the specified amount of time. | |
static void | sleepUntil (const Time &timeout) throw (UnexpectedException) |
Make the calling thread sleep until the specified point in time has been reached. | |
static void | acceptTermination () throw (UnexpectedException) |
This method should be called by a thread if it is executing lengthy computations without any calls to Condition::wait, Condition::timedWait or Thread::sleep. | |
static void | selfResurrect () |
Recover from termination. | |
static void | mainExitWait () |
When called by the main thread it suspends the main thread until all other threads have terminated. | |
Protected Member Functions | |
virtual void | main ()=0 |
This is the method that will be run as the new thread. | |
Thread () | |
Does not start the thread. | |
virtual | ~Thread () |
Classes | |
struct | AlreadyStartedException |
struct | NotStartedException |
struct | SelfThread |
struct | ThreadException |
You may think of it as an object with a will of its own or an animated object. Just as in the case of humans, going from one to two or more threads of execution opens up a whole new realm of wonders and disasters. So, prepare yourself.
This class supports safe cancelation/termination of threads. The problem of thread cancelation usually is that it is hard to guarantee proper cleanup of allocated memory and other resources. This is especially true in C++ programs where it is crucial that the stack is noramlly unwinded ensuring that class detructors gets called. So what we really need is to allow one thread to provoke an exception in another thread. This is exactly what this thread class supports. If a thread gets a terminate request, then any current or future calls to any of the following methods will raise a TerminatedException.
Semaphore::down Condition::wait, Condition::timedWait, Condition::select, Thread::wait, Thread::sleep, Thread::acceptTermination,
Definition at line 98 of file thread.H.
|
Does not start the thread. You must use Thread::start to do that - BUT NOT WITHIN THE COSTRUCTOR! |
|
This method should be called by a thread if it is executing lengthy computations without any calls to Condition::wait, Condition::timedWait or Thread::sleep. This is to ensure a fair response time to a termination request by another thread. Note, the more frequent this method is called the shorter the response to a tremination request will be.
|
|
This is the method that will be run as the new thread. You should override this method. Implemented in T, Producer, Consumer, Archon::Utilities::Stream::Connector< C >, Archon::Utilities::Thread::SelfThread, Archon::Utilities::MethodVoidRunner< T >, Archon::Utilities::MethodArgRunner< T, U >, Archon::Utilities::FuncVoidRunner, Archon::Utilities::FuncArgRunner< T >, Archon::X3D::SAI::ApplicationSceneReaper, and Archon::X3D::SAI::Session::ExternalEventQueue. |
|
When called by the main thread it suspends the main thread until all other threads have terminated. The intention is that you should call this method just before returning from the main function of your program. The intended purpose of this method is enforce proper deallocation of all resources by ensuring that all stacks are unwinded completely before your program exits. It makes no sence to call this method for any other thread than the main thread, since when the main thread terminates it will imadiately annihilate all other threads, preventing them from proper stack unwinding. Theads which are not created by one of the methods in this Thread class and which have never called the Thread::self method are unknown to this method, and will therefore not be waited for. Definition at line 309 of file thread.C. References self(). Referenced by Archon::Console3d::main(). |
|
Let a new thread invoke a static void-function of your choise.
Definition at line 430 of file thread.H. References start(). |
|
Let a new thread invoke a static 1-argument-function of your choise.
Definition at line 455 of file thread.H. References start(). |
|
Like Thread::run(Ref<T>, void (T::*)()) except that it invokes a method taking one argument, and you must specify that extra argument when calling this method.
Definition at line 414 of file thread.H. References start(). |
|
Let a new thread invoke a method of your choise on a reference counted object of your choise. An extra reference count is held during the execution of the specified method, and it is released afterwards.
Definition at line 385 of file thread.H. References start(). Referenced by Archon::Utilities::Job::Queue::add(), and Archon::Utilities::Web::DefaultClient::request(). |
|
Return a reference to the Thread object associated with the thread that calls this method. This also works for the main thread and any other thread created in the context of the pthead library.
Definition at line 250 of file thread.C. Referenced by mainExitWait(), Archon::Utilities::Condition::select(), Archon::Utilities::Condition::timedWait(), and Archon::Utilities::Condition::wait(). |
|
Recover from termination. That is, you are allowed to catch the TerminatedException in your thread if you do not want your thread to terminate completely. But note that this may be in conflict with the intent of some other thread calling terminate on this thread.
|
|
Make the calling thread sleep for the specified amount of time.
Definition at line 286 of file thread.C. References Archon::SaiTestApps::Filebrowser::mutex, Archon::Utilities::Time::now(), and Archon::Utilities::Condition::timedWait(). Referenced by Archon::SaiTestApps::Spot::main(), Archon::SaiTestApps::Simple::main(), Archon::SaiTestApps::Molecule::main(), Archon::SaiTestApps::Cylinders::main(), Archon::SaiTestApps::Clock::main(), Archon::Raytracer::Engine::render(), Archon::Raytracer::viewScene(), and Archon::Console3d::viewScene(). |
|
Make the calling thread sleep until the specified point in time has been reached.
Definition at line 296 of file thread.C. References Archon::SaiTestApps::Filebrowser::mutex, and Archon::Utilities::Condition::timedWait(). Referenced by Archon::Render::Test::main(), and Archon::Display::Test::main(). |
|
Let a new thread invoke the virtual main method of the specified Thread object. To be of any use, you must derive your own class from the Thread object. An extra reference count is held on the thread object during the execution of the specified method, and it is released when execution ends.
Definition at line 208 of file thread.C. Referenced by Archon::Render::Conductor::addPipe(), and run(). |
|
Issue a termination request for this thread. This causes any present or furure calls to Semaphore::down Condition::wait, Condition::timedWait, Condition::select, Thread::wait, Thread::sleep, Thread::acceptTermination, by this thread to imediately throw a TerminatedException. This method will normally be called by some other thread in the intent to bring the execution of this thread to an end.
Definition at line 262 of file thread.C. References Archon::Utilities::Condition::mutex, Archon::Utilities::Condition::notifyAll(), and Archon::Utilities::Mutex::Lock::release(). |
|
Wait for this thread to stop.
Definition at line 279 of file thread.C. References Archon::Utilities::Condition::wait(). Referenced by T::~T(). |