GNE
0.75.0
|
The timer class is used to get the current time and to provide callbacks. More...
#include <Timer.h>
Public Types | |
typedef SmartPtr< Timer > | sptr |
typedef WeakPtr< Timer > | wptr |
![]() | |
enum | ThreadType { USER, TIMER, SYSTEM, CONNECTION, ALL } |
An enum for specifying the thread type. More... | |
typedef SmartPtr< Thread > | sptr |
typedef WeakPtr< Thread > | wptr |
Public Member Functions | |
virtual | ~Timer () |
Destructor. | |
SmartPtr< TimerCallback > | getCallback () const |
Returns the TimerCallback set in the constructor. | |
void | startTimer () |
Starts the timer running and calling the callback. | |
virtual void | shutDown () |
Equivalent to calling stopTimer with false. | |
void | stopTimer (bool waitForEnd) |
Stops the timer and stops calling the callback. | |
![]() | |
Thread (std::string name=DEF_NAME, int priority=DEF_PRI) | |
Creates a new thread, ready to run but not yet running. | |
std::string | getName () const |
Returns the name of this thread. | |
void | join () |
A blocking call that returns when this thread terminates, or returns immediately if the thread has terminated. | |
bool | hasStarted () const |
Returns true if this Thread has ever been started. | |
bool | isRunning () const |
Determine the running state of the thread. | |
void | start () |
Starts this thread running. | |
int | getPriority () const |
Returns the priority of this class. |
Static Public Member Functions | |
static void | stopAll () |
Stops all Timers. | |
static sptr | create (const SmartPtr< TimerCallback > &callback, int rate) |
Create a timer callback. | |
static Time | getCurrentTime () |
Returns the current time from some arbitrary point in the past. | |
static Time | getAbsoluteTime () |
Returns the current time from the system clock. | |
![]() | |
static sptr | currentThread () |
Returns the Thread object that represents this calling thread. | |
static void | sleep (int ms) |
The currently running thread sleeps for the time given in milliseconds. | |
static void | yield () |
Makes a request to the operating system to give up the remainder of this thread's timeslice to another appropriate thread. | |
static bool | waitForAllThreads (int ms) |
This method will wait for all threads that have a type != SYSTEM. | |
static void | requestAllShutdown (ThreadType threadType) |
Calls the shutDown method of all running threads of the given type. |
Protected Member Functions | |
Timer (const SmartPtr< TimerCallback > &callback, int rate) | |
void | run () |
This is the thread that will perform the callbacks. | |
![]() | |
void | setThisPointer (const wptr &thisPtr) |
IMPORTANT: call this method in your static create function. | |
sptr | getThisPointer () const |
Returns a SmartPtr to this object. | |
void | setType (ThreadType newType) |
Sets this Thread's type. |
Additional Inherited Members | |
![]() | |
static const int | DEF_PRI = 0 |
The default priority of a thread. | |
static const std::string | DEF_NAME = "Thread" |
The default name for a thread. | |
static const int | LOW_PRI = -1 |
A lowered priority for a thread. | |
static const int | LOWER_PRI = -2 |
A lower priority for a thread than LOW_PRI. | |
static const int | HIGH_PRI = 1 |
A "boosted priority" for a thread. | |
static const int | HIGHER_PRI = 2 |
Even higher priority thread than HIGH_PRI. | |
![]() | |
volatile bool | shutdown |
This variable is set to true when this thread should exit. |
The timer class is used to get the current time and to provide callbacks.
A timer object calls its listeners back every so often based on the time given.
All of the methods in this class are safe to call from multiple threads at the same time, and can also be called from the TimerCallback as well, with a few (some obvious) exceptions.
A Timer thread runs with a higher priority than the main thread, therefore its callbacks are suitable for short, quick tasks.
|
static |
Create a timer callback.
The first call to the callback will occur after "rate" milliseconds, so this class is suitable for setting timeouts for your operations. Use the startTimer method to start this timer.
The callback is released when the Timer is stopped. This allows the callback to contain a reference to the Timer.
callback | A newly allocated object to perform callbacks on. |
rate | the callback rate in milliseconds. |
|
static |
Returns the current time from the system clock.
The time returned is an absolute time, relative to midnight, Jan 1, 1970.
TimerCallback::sptr GNE::Timer::getCallback | ( | ) | const |
Returns the TimerCallback set in the constructor.
After the timer has stopped, an empty SmartPtr is returned since the listener is released when the timer stops.
|
static |
Returns the current time from some arbitrary point in the past.
This is usually a very high precision timer that likely provides microsecond or better resolution.
void GNE::Timer::startTimer | ( | ) |
void GNE::Timer::stopTimer | ( | bool | waitForEnd | ) |
Stops the timer and stops calling the callback.
The timer will likely be called one more time because the timer will actually stop at the end of its most recent cycle. If you want to wait until the callback is called for the last time, pass true into this function. Then this function will block for at most the time of the callback rate plus the time it takes for the callback to finish.
This timer's callback can call this function, but obviously it must not pass true to this function.
If a Timer is already stopped, this function will have no effect.