YARP
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:

Detailed Description

An abstraction for a periodic thread.

Deprecated:
since YARP 3.0.0

Definition at line 36 of file 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...
 

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

◆ ~RateThread()

RateThread::~RateThread ( )
virtualdefault

Member Function Documentation

◆ afterStart()

void RateThread::afterStart ( bool  success)
overrideprotectedvirtual

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 from yarp::os::PeriodicThread.

Definition at line 125 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 47 of file RateThread.cpp.

◆ beforeStart()

void RateThread::beforeStart ( )
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 121 of file RateThread.cpp.

◆ getEstPeriod() [1/2]

double RateThread::getEstPeriod ( )

Return estimated period since last reset.

Definition at line 83 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 93 of file RateThread.cpp.

◆ getEstUsed() [1/2]

double RateThread::getEstUsed ( )

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

Definition at line 88 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 100 of file RateThread.cpp.

◆ getIterations()

unsigned int RateThread::getIterations ( )

Return the number of iterations performed since last reset.

Definition at line 78 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 thread.

Definition at line 140 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 135 of file RateThread.cpp.

◆ getRate()

double RateThread::getRate ( )

Return the current rate of the thread.

Returns
thread current rate [ms].

Definition at line 32 of file RateThread.cpp.

◆ isRunning()

bool RateThread::isRunning ( )

Returns true when the thread is started, false otherwise.

Definition at line 63 of file RateThread.cpp.

◆ isSuspended()

bool RateThread::isSuspended ( )

Returns true when the thread is suspended, false otherwise.

Definition at line 37 of file RateThread.cpp.

◆ resetStat()

void RateThread::resetStat ( )

Reset thread statistics.

Definition at line 107 of file RateThread.cpp.

◆ resume()

void RateThread::resume ( )

Resume the thread if previously suspended.

Definition at line 73 of file RateThread.cpp.

◆ run()

void yarp::os::RateThread::run ( )
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.

◆ 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 policy is highly OS dependent 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 130 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 27 of file RateThread.cpp.

◆ start()

bool RateThread::start ( )

Call this to start the thread.

Blocks until initThread() is executed.

Definition at line 58 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 52 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 42 of file RateThread.cpp.

◆ suspend()

void RateThread::suspend ( )

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

Definition at line 68 of file RateThread.cpp.

◆ threadInit()

bool RateThread::threadInit ( )
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 112 of file RateThread.cpp.

◆ threadRelease()

void RateThread::threadRelease ( )
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 117 of file RateThread.cpp.


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