An abstraction for a periodic thread. More...
#include <yarp/os/RateThread.h>
Public Member Functions | |
RateThread (int period) | |
Constructor. More... | |
virtual | ~RateThread () |
bool | start () |
Call this to start the thread. More... | |
bool | step () |
Call this to "step" the thread rather than starting it. More... | |
void | stop () |
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() called). More... | |
void | askToStop () |
Stop the thread. More... | |
bool | isRunning () |
Returns true when the thread is started, false otherwise. More... | |
bool | isSuspended () |
Returns true when the thread is suspended, false otherwise. More... | |
bool | setRate (int period) |
Set the (new) rate of the thread. More... | |
double | getRate () |
Return the current rate of the thread. More... | |
void | suspend () |
Suspend the thread, the thread keeps running by doLoop is never executed. More... | |
void | resume () |
Resume the thread if previously suspended. More... | |
void | resetStat () |
Reset thread statistics. More... | |
double | getEstPeriod () |
Return estimated period since last reset. More... | |
void | getEstPeriod (double &av, double &std) |
Return estimated period since last reset. More... | |
unsigned int | getIterations () |
Return the number of iterations performed since last reset. More... | |
double | getEstUsed () |
Return the estimated duration of the run() function since last reset. More... | |
void | getEstUsed (double &av, double &std) |
Return estimated duration of the run() function since last reset. More... | |
int | setPriority (int priority, int policy=-1) |
Set the priority and scheduling policy of the thread, if the OS supports that. More... | |
int | getPriority () |
Query the current priority of the thread, if the OS supports that. More... | |
int | getPolicy () |
Query the current scheduling policy of the thread, if the OS supports that. More... | |
Protected Member Functions | |
bool | threadInit () override |
Initialization method. More... | |
void | threadRelease () override |
Release method. More... | |
void | run () override=0 |
Loop function. More... | |
void | beforeStart () override |
Called just before a new thread starts. More... | |
void | afterStart (bool success) override |
Called just after a new thread starts (or fails to start), this is executed by the same thread that calls start(). More... | |
An abstraction for a periodic thread.
Definition at line 38 of file RateThread.h.
RateThread::RateThread | ( | int | period | ) |
Constructor.
Thread begins in a dormant state. Call RateThread::start to get things going.
period | The period in milliseconds [ms] between successive calls to the RateThread::run method (remember you need to call RateThread::start first before anything happens) |
Definition at line 26 of file RateThread.cpp.
|
virtualdefault |
|
overrideprotectedvirtual |
Called just after a new thread starts (or fails to start), this is executed by the same thread that calls start().
success | true iff the new thread started successfully. |
Reimplemented from yarp::os::PeriodicThread.
Definition at line 131 of file RateThread.cpp.
void RateThread::askToStop | ( | ) |
Stop the thread.
Like stop but it does not call join, safe to be called from run().
Definition at line 53 of file RateThread.cpp.
|
overrideprotectedvirtual |
Called just before a new thread starts.
This method is executed by the same thread that calls start().
Reimplemented from yarp::os::PeriodicThread.
Definition at line 127 of file RateThread.cpp.
double RateThread::getEstPeriod | ( | ) |
Return estimated period since last reset.
Definition at line 89 of file RateThread.cpp.
void RateThread::getEstPeriod | ( | double & | av, |
double & | std | ||
) |
Return estimated period since last reset.
av | average value |
std | standard deviation |
Definition at line 99 of file RateThread.cpp.
double RateThread::getEstUsed | ( | ) |
Return the estimated duration of the run() function since last reset.
Definition at line 94 of file RateThread.cpp.
void RateThread::getEstUsed | ( | double & | av, |
double & | std | ||
) |
Return estimated duration of the run() function since last reset.
av | average value |
std | standard deviation |
Definition at line 106 of file RateThread.cpp.
unsigned int RateThread::getIterations | ( | ) |
Return the number of iterations performed since last reset.
Definition at line 84 of file RateThread.cpp.
int RateThread::getPolicy | ( | ) |
Query the current scheduling policy of the thread, if the OS supports that.
Definition at line 146 of file RateThread.cpp.
int RateThread::getPriority | ( | ) |
Query the current priority of the thread, if the OS supports that.
Definition at line 141 of file RateThread.cpp.
double RateThread::getRate | ( | ) |
Return the current rate of the thread.
Definition at line 38 of file RateThread.cpp.
bool RateThread::isRunning | ( | ) |
Returns true when the thread is started, false otherwise.
Definition at line 69 of file RateThread.cpp.
bool RateThread::isSuspended | ( | ) |
Returns true when the thread is suspended, false otherwise.
Definition at line 43 of file RateThread.cpp.
void RateThread::resetStat | ( | ) |
Reset thread statistics.
Definition at line 113 of file RateThread.cpp.
void RateThread::resume | ( | ) |
Resume the thread if previously suspended.
Definition at line 79 of file RateThread.cpp.
|
overrideprotectedpure virtual |
Loop function.
This is the thread itself. The thread calls the run() function every <period> ms. At the end of each run, the thread will sleep the amounth of time required, taking into account the time spent inside the loop function. Example: requested period is 10ms, the run() function take 3ms to be executed, the thread will sleep for 7ms.
Note: after each run is completed, the thread will call a yield() in order to facilitate other threads to run.
Implements yarp::os::PeriodicThread.
int RateThread::setPriority | ( | int | priority, |
int | policy = -1 |
||
) |
Set the priority and scheduling policy of the thread, if the OS supports that.
priority | the new priority of the thread. |
policy | the scheduling policy of the thread |
Definition at line 136 of file RateThread.cpp.
bool RateThread::setRate | ( | int | period | ) |
Set the (new) rate of the thread.
period | the rate [ms] |
Definition at line 33 of file RateThread.cpp.
bool RateThread::start | ( | ) |
Call this to start the thread.
Blocks until initThread() is executed.
Definition at line 64 of file RateThread.cpp.
bool RateThread::step | ( | ) |
Call this to "step" the thread rather than starting it.
This will execute at most one call to doLoop before returning.
Definition at line 58 of file RateThread.cpp.
void RateThread::stop | ( | ) |
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() called).
Actually calls join. This will deadlock if called from run(), use askToStop() instead.
Definition at line 48 of file RateThread.cpp.
void RateThread::suspend | ( | ) |
Suspend the thread, the thread keeps running by doLoop is never executed.
Definition at line 74 of file RateThread.cpp.
|
overrideprotectedvirtual |
Initialization method.
The thread executes this function when it starts and before "run". This is a good place to perform initialization tasks that need to be done by the thread itself (device drivers initialization, memory allocation etc). If the function returns false the thread quits and never calls "run". The return value of threadInit() is notified to the class and passed as a parameter to afterStart(). Note that afterStart() is called by the same thread that is executing the "start" method.
Reimplemented from yarp::os::PeriodicThread.
Definition at line 118 of file RateThread.cpp.
|
overrideprotectedvirtual |
Release method.
The thread executes this function once when it exits, after the last "run". This is a good place to release resources that were initialized in threadInit() (release memory, and device driver resources).
Reimplemented from yarp::os::PeriodicThread.
Definition at line 123 of file RateThread.cpp.