Thread of example plugin. More...
#include <plugins/examples/basics/thread.h>
Public Member Functions | |
ExampleThread (fawkes::BlockedTimingAspect::WakeupHook hook, const char *name, unsigned int modc) | |
Constructor. | |
virtual | ~ExampleThread () |
Destructor. | |
virtual void | init () |
Initialize the thread. | |
virtual void | loop () |
Thread loop. | |
virtual void | finalize () |
Finalize the thread. | |
Protected Member Functions | |
virtual void | run () |
Stub to see name in backtrace for easier debugging. |
Thread of example plugin.
Definition at line 30 of file thread.h.
ExampleThread::ExampleThread | ( | fawkes::BlockedTimingAspect::WakeupHook | hook, | |
const char * | name, | |||
unsigned int | modc | |||
) |
Constructor.
hook | hook to register this thread for | |
name | thread name | |
modc | modulo count, every modc iterations a message is printed to stdout |
Definition at line 40 of file thread.cpp.
ExampleThread::~ExampleThread | ( | ) | [virtual] |
Destructor.
We cannot do the following: logger->log_info("ExampleThread", "Destroying thread %s", name());
The reason: We do not know if this thread has been successfully initialized. It could be, that any other thread that is in the same thread list as this thread failed to initialize, before the current thread has been initialized. In this case the LoggingAspect has not been initialized and thus logger is undefined and this would cause a fatal segfault.
Definition at line 51 of file thread.cpp.
void ExampleThread::finalize | ( | ) | [virtual] |
Finalize the thread.
This method is executed just before the thread is canceled and destroyed. It is always preceeded by a call to prepare_finalize(). If this is not the case this is a failure. The condition can be checked with the boolean variable finalize_prepared.
This method is meant to be used in conjunction with aspects and to cover thread inter-dependencies. This routine MUST bring the thread into a safe state such that it may be canceled and destroyed afterwards. If there is any reason that this cannot happen make your prepare_finalize() reports so.
This method is called by the thread manager just before the thread is being cancelled. Here you can do whatever steps are necessary just before the thread is cancelled. Note that you thread is still running and might be in the middle of a loop, so it is not a good place to give up on all resources used. Mind segmentation faults that could happen. Protect the area with a mutex that you lock at the beginning of your loop and free in the end, and that you lock at the beginning of finalize and then never unlock. Also not that the finalization may be canceled afterwards. The next thing that happens is that either the thread is canceled and destroyed or that the finalization is canceled and the thread has to run again.
Finalize is called on a thread just before it is deleted. It is guaranteed to be called on a fully initialized thread (if no exception is thrown in init()) (this guarantee holds in the Fawkes framework).
The default implementation does nothing besides throwing an exception if prepare_finalize() has not been called.
Exception | thrown if prepare_finalize() has not been called. |
Reimplemented from fawkes::Thread.
Definition at line 79 of file thread.cpp.
References fawkes::Logger::log_info(), fawkes::LoggingAspect::logger, and fawkes::Thread::name().
void ExampleThread::init | ( | ) | [virtual] |
Initialize the thread.
This method is meant to be used in conjunction with aspects. Some parts of the initialization may only happen after some aspect of the thread has been initialized. Implement the init method with these actions. It is guaranteed to be called just after all aspects have been initialized and only once in the lifetime of the thread. Throw an exception if any problem occurs and the thread should not run.
Just because your init() routine suceeds and everything looks fine for this thread does not automatically imply that it will run. If it belongs to a group of threads in a ThreadList and any of the other threads fail to initialize then no thread from this group is run and thus this thread will never run. In that situation finalize() is called for this very instance, prepare_finalize() however is not called.
Reimplemented from fawkes::Thread.
Definition at line 66 of file thread.cpp.
References fawkes::Logger::log_info(), fawkes::LoggingAspect::logger, and fawkes::Thread::name().
void ExampleThread::loop | ( | ) | [virtual] |
Thread loop.
If num iterations module modc is 0 print out messaege, otherwise do nothing.
Reimplemented from fawkes::Thread.
Definition at line 89 of file thread.cpp.
References fawkes::Logger::log_info(), fawkes::LoggingAspect::logger, and fawkes::Thread::name().
virtual void ExampleThread::run | ( | ) | [inline, protected, virtual] |
Stub to see name in backtrace for easier debugging.
Reimplemented from fawkes::Thread.