YARP  2.3.70.1
Yet Another Robot Platform
yarp::os::RateThread Class Referenceabstract

An abstraction for a periodic thread. More...

#include <yarp/os/RateThread.h>

+ Inheritance diagram for yarp::os::RateThread:

Public Member Functions

 RateThread (int period)
 Constructor. More...
 
virtual ~RateThread ()
 
virtual bool threadInit ()
 Initialization method. More...
 
virtual void threadRelease ()
 Release method. More...
 
virtual void run ()=0
 Loop function. More...
 
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...
 
virtual void beforeStart ()
 Called just before a new thread starts. More...
 
virtual void afterStart (bool success)
 Called just after a new thread starts (or fails to start), this is executed by the same thread that calls start(). 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...
 

Private Member Functions

bool join (double seconds=-1)
 

Private Attributes

void * implementation
 

Detailed Description

An abstraction for a periodic thread.

Examples:
os/ratethread.cpp.

Definition at line 24 of file RateThread.h.

Constructor & Destructor Documentation

◆ RateThread()

RateThread::RateThread ( int  period)

Constructor.

Thread begins in a dormant state. Call RateThread::start to get things going.

Parameters
periodThe 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 239 of file RateThread.cpp.

◆ ~RateThread()

RateThread::~RateThread ( )
virtual

Definition at line 246 of file RateThread.cpp.

Member Function Documentation

◆ afterStart()

void RateThread::afterStart ( bool  success)
virtual

Called just after a new thread starts (or fails to start), this is executed by the same thread that calls start().

Parameters
successtrue iff the new thread started successfully.

Reimplemented in yarp::os::RateThreadWrapper.

Definition at line 351 of file RateThread.cpp.

◆ askToStop()

void RateThread::askToStop ( )

Stop the thread.

Like stop but it does not call join, safe to be called from run().

Definition at line 279 of file RateThread.cpp.

◆ beforeStart()

void RateThread::beforeStart ( )
virtual

Called just before a new thread starts.

This method is executed by the same thread that calls start().

Reimplemented in yarp::os::RateThreadWrapper.

Definition at line 348 of file RateThread.cpp.

◆ getEstPeriod() [1/2]

double RateThread::getEstPeriod ( )

Return estimated period since last reset.

Definition at line 315 of file RateThread.cpp.

◆ getEstPeriod() [2/2]

void RateThread::getEstPeriod ( double &  av,
double &  std 
)

Return estimated period since last reset.

Parameters
avaverage value
stdstandard deviation

Definition at line 325 of file RateThread.cpp.

◆ getEstUsed() [1/2]

double RateThread::getEstUsed ( )

Return the estimated duration of the run() function since last reset.

Definition at line 320 of file RateThread.cpp.

◆ getEstUsed() [2/2]

void RateThread::getEstUsed ( double &  av,
double &  std 
)

Return estimated duration of the run() function since last reset.

Parameters
avaverage value
stdstandard deviation

Definition at line 330 of file RateThread.cpp.

◆ getIterations()

unsigned int RateThread::getIterations ( )

Return the number of iterations performed since last reset.

Definition at line 310 of file RateThread.cpp.

◆ getPolicy()

int RateThread::getPolicy ( )

Query the current scheduling policy of the thread, if the OS supports that.

Returns
the scheduling policy of the theread.

Definition at line 364 of file RateThread.cpp.

◆ getPriority()

int RateThread::getPriority ( )

Query the current priority of the thread, if the OS supports that.

Returns
the priority of the thread.

Definition at line 359 of file RateThread.cpp.

◆ getRate()

double RateThread::getRate ( )

Return the current rate of the thread.

Returns
thread current rate [ms].

Definition at line 259 of file RateThread.cpp.

◆ isRunning()

bool RateThread::isRunning ( )

Returns true when the thread is started, false otherwise.

Definition at line 295 of file RateThread.cpp.

◆ isSuspended()

bool RateThread::isSuspended ( )

Returns true when the thread is suspended, false otherwise.

Definition at line 264 of file RateThread.cpp.

◆ join()

bool RateThread::join ( double  seconds = -1)
private

Definition at line 269 of file RateThread.cpp.

◆ resetStat()

void RateThread::resetStat ( )

Reset thread statistics.

Definition at line 335 of file RateThread.cpp.

◆ resume()

void RateThread::resume ( )

Resume the thread if previously suspended.

Definition at line 305 of file RateThread.cpp.

◆ run()

◆ setPriority()

int RateThread::setPriority ( int  priority,
int  policy = -1 
)

Set the priority and scheduling policy of the thread, if the OS supports that.

Parameters
prioritythe new priority of the thread.
policythe scheduling policy of the thread
Returns
-1 if the priority cannot be set.
Note
The thread plolicy is highly OS dependant and a right combination of priority and policy should be used. In some platform changing thread priorities is subject to having the right permission. For example, the following combinations are supported on most Linux platforms: SCHED_OTHER : policy=0, priority=[0 .. 0] SCHED_FIFO : policy=1, priority=[1 .. 99] SCHED_RR : policy=2, priority=[1 .. 99]

Definition at line 354 of file RateThread.cpp.

◆ setRate()

bool RateThread::setRate ( int  period)

Set the (new) rate of the thread.

Parameters
periodthe rate [ms]
Returns
true.

Definition at line 254 of file RateThread.cpp.

◆ start()

bool RateThread::start ( )

Call this to start the thread.

Blocks until initThread() is executed.

Definition at line 290 of file RateThread.cpp.

◆ step()

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 284 of file RateThread.cpp.

◆ stop()

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 274 of file RateThread.cpp.

◆ suspend()

void RateThread::suspend ( )

Suspend the thread, the thread keeps running by doLoop is never executed.

Definition at line 300 of file RateThread.cpp.

◆ threadInit()

bool RateThread::threadInit ( )
virtual

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 in yarp::dev::ServerGrabber, yarp::dev::RGBDSensorWrapper, yarp::os::RateThreadWrapper, yarp::dev::AnalogWrapper, yarp::dev::BoschIMU, RpLidar, yarp::dev::FrameTransformClient, yarp::dev::JoypadControlServer, FakeLaser, yarp::dev::FrameTransformServer, laserHokuyo, yarp::dev::Rangefinder2DWrapper, LaserFromDepth, RpLidar2, yarp::dev::BatteryWrapper, yarp::dev::FakeAnalogSensor, yarp::os::PriorityDebugThread, yarp::dev::fakeIMU, and yarp::dev::OVRHeadset.

Definition at line 340 of file RateThread.cpp.

◆ threadRelease()

void RateThread::threadRelease ( )
virtual

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 in yarp::dev::ServerGrabber, yarp::dev::RGBDSensorWrapper, yarp::os::RateThreadWrapper, yarp::dev::AnalogWrapper, yarp::dev::BoschIMU, RpLidar, yarp::dev::FrameTransformClient, yarp::dev::JoypadControlServer, FakeLaser, yarp::dev::FrameTransformServer, laserHokuyo, yarp::dev::Rangefinder2DWrapper, LaserFromDepth, RpLidar2, yarp::dev::BatteryWrapper, yarp::dev::FakeAnalogSensor, yarp::os::PriorityDebugThread, and yarp::dev::OVRHeadset.

Definition at line 345 of file RateThread.cpp.

Member Data Documentation

◆ implementation

void* yarp::os::RateThread::implementation
private

Definition at line 204 of file RateThread.h.


The documentation for this class was generated from the following files: