#include <ithread.h>
Inheritance diagram for ithread:


Public Types | |
| enum | timed_thread_types { TIGHT_INTERVAL, SLACK_INTERVAL } |
Public Member Functions | |
| ithread () | |
| creates a single-shot thread object. | |
| ithread (int sleep_timer, timed_thread_types how=SLACK_INTERVAL) | |
| creates a managed thread object that runs on a periodic interval. | |
| virtual | ~ithread () |
| IMPLEMENT_CLASS_NAME ("ithread") | |
| bool | start (void *thread_data) |
| causes the thread to start, if it has not already been started. | |
| void | stop () |
| tells the thread to shutdown and waits for the shutdown to occur. | |
| void | cancel () |
| stops the thread but does not wait until it has terminated. | |
| virtual void | pre_thread () |
| invoked just after after start(), when the OS thread is created. | |
| virtual void | post_thread () |
| this is invoked just before the thread is to be terminated. | |
| virtual void | perform_activity (void *thread_data)=0 |
| carries out the main activity of the thread. | |
| void | exempt_stop () |
| this special form of stop() does not wait for the thread to exit. | |
| void | reschedule (int delay) |
| causes a periodic thread to activate after "delay" milliseconds from now. | |
| void | reschedule () |
| causes a periodic thread to activate as soon as possible. | |
| int | sleep_time () const |
| returns the current periodic thread interval. | |
| void | sleep_time (int new_sleep) |
| adjusts the period for the thread to the "new_sleep" interval. | |
| bool | thread_started () const |
| returns true if the thread has been started. | |
| bool | thread_finished () const |
| returns true if the thread has exited. | |
| bool | thread_active () const |
| returns true if the thread is currently performing its activity. | |
| bool | should_stop () const |
| reports whether the thread should stop right now. | |
This greatly simplifies creating and managing threads by hiding all the operating system details. The user just needs to override one virtual function in their derived object to perform the main activity of their thread. The thread can be a one time invocation or it can run periodically. Control over the thread remains in the hands of the program that started it.
Definition at line 38 of file ithread.h.
| ithread::ithread | ( | ) |
creates a single-shot thread object.
the OS-level thread is not started until the start() method is invoked. this constructor creates a thread that will only execute once; when start() is called, the thread starts up and performs its activity. it will then stop. to run it again, start() must be invoked again. however, if the perform_activity() method just keeps running, then the single-shot thread can live as long as needed. it is important for such a thread to periodically check should_exit() to avoid having the program hang-up when it's supposed to be shutting down.
Definition at line 82 of file ithread.cpp.
References FUNCDEF.
| ithread::ithread | ( | int | sleep_timer, | |
| timed_thread_types | how = SLACK_INTERVAL | |||
| ) |
creates a managed thread object that runs on a periodic interval.
the thread will activate every "sleep_timer" milliseconds. when start() is invoked, the thread's action (via the perform_activity() method) will be performed at regular intervals (using the specified value for "sleep_timer"). the thread will continue activating until the stop() method is called. a faster interval is used internally during sleep periods such that calling stop() will not consume the whole "sleep_timer" period. if the "how" is TIGHT_INTERVAL, then the thread will activate every "sleep_timer" milliseconds, as accurately as possible. if the "how" is SLACK_INTERVAL, then the thread will activate after a delay of "sleep_timer" milliseconds from its last activation. the latter mode allows the thread to consume its entire intended operation time knowing that there will still be slack time between when it is active. the former mode requires the thread to only run for some amount of time less than its "sleep_timer"; otherwise it will hog a lot of the CPU.
Definition at line 100 of file ithread.cpp.
References FUNCDEF, and MINIMUM_SLEEP_PERIOD.
| ithread::~ithread | ( | ) | [virtual] |
| ithread::IMPLEMENT_CLASS_NAME | ( | "ithread" | ) |
| bool ithread::start | ( | void * | thread_data | ) |
causes the thread to start, if it has not already been started.
if the thread has terminated previously, then this will restart the thread. true is returned if the thread could be started. false is returned if the thread could not be started or if it is already running.
Definition at line 147 of file ithread.cpp.
References exempt_stop(), FUNCDEF, LOG, MAXIMUM_CREATE_ATTEMPTS, time_stamp::reset(), portable::sleep_ms(), SNOOZE_FOR_RETRY, portable::system_error(), portable::system_error_text(), and thread_finished().
Referenced by thread_cabinet::add_thread().
| void ithread::stop | ( | ) |
tells the thread to shutdown and waits for the shutdown to occur.
this will cause the OS thread to terminate once the current (if any) perform_activity() invocation completes. the thread may be restarted with start().
Definition at line 196 of file ithread.cpp.
References cancel(), exempt_stop(), portable::sleep_ms(), thread_finished(), and thread_started().
Referenced by stdio_redirecter::zap_program(), and ~ithread().
| void ithread::cancel | ( | ) | [inline] |
stops the thread but does not wait until it has terminated.
this is appropriate for use within the perform_activity() method.
Definition at line 87 of file ithread.h.
Referenced by stop(), and stdio_redirecter::zap_program().
| void ithread::pre_thread | ( | ) | [virtual] |
invoked just after after start(), when the OS thread is created.
the call comes in _from_ the thread itself, so the derived method must be thread-safe.
Definition at line 130 of file ithread.cpp.
| void ithread::post_thread | ( | ) | [virtual] |
this is invoked just before the thread is to be terminated.
the call also comes in from the thread itself, so the implementation must be thread-safe.
Definition at line 132 of file ithread.cpp.
| virtual void ithread::perform_activity | ( | void * | thread_data | ) | [pure virtual] |
carries out the main activity of the thread.
this is called repeatedly by the main thread management function and so should return as soon as possible. if it does not return fairly regularly, then the thread shutdown process will not occur until the function exits on its own.
Implemented in managed_thread.
| void ithread::exempt_stop | ( | ) |
this special form of stop() does not wait for the thread to exit.
it is required in certain weird OS situations where the thread does not exit properly and stop() would cause an infinite wait. don't use it unless you are SURE that this is the case.
Definition at line 213 of file ithread.cpp.
| void ithread::reschedule | ( | int | delay | ) |
causes a periodic thread to activate after "delay" milliseconds from now.
this resets the normal activation period, but after the next activation occurs, the normal activation interval takes over again.
Definition at line 142 of file ithread.cpp.
| void ithread::reschedule | ( | ) |
causes a periodic thread to activate as soon as possible.
this occurs regardless of its normal timing. once the activation occurs, then the thread returns to a normal schedule.
Definition at line 137 of file ithread.cpp.
| int ithread::sleep_time | ( | ) | const [inline] |
| void ithread::sleep_time | ( | int | new_sleep | ) | [inline] |
| bool ithread::thread_started | ( | ) | const [inline] |
| bool ithread::thread_finished | ( | ) | const [inline] |
returns true if the thread has exited.
This can happen either by the thread responding to the stop() or cancel() methods or when the thread stops of its own accord. if this returns true, it means that the thread will not start up again unless the user invokes start().
Definition at line 133 of file ithread.h.
Referenced by thread_cabinet::add_thread(), start(), and stop().
| bool ithread::thread_active | ( | ) | const [inline] |
| bool ithread::should_stop | ( | ) | const [inline] |
reports whether the thread should stop right now.
this returns true due to an invocation of stop() or cancel().
Definition at line 144 of file ithread.h.
Referenced by post_office::deliver_mail_on_route(), and cromp_server::look_for_clients().
1.5.1