YARP
Yet Another Robot Platform
yarp::os::PeriodicThread Class Referenceabstract

An abstraction for a periodic thread. More...

#include <yarp/os/PeriodicThread.h>

Inherited by GenericSensorRosPublisher< yarp::rosmsg::geometry_msgs::WrenchStamped >, GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::Temperature >, GenericSensorRosPublisher< yarp::rosmsg::geometry_msgs::PoseStamped >, GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::MagneticField >, GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::Imu >, AnalogWrapper, AudioPlayerWrapper, AudioRecorderDataThread, AudioRecorderStatusThread, BatteryWrapper, BoschIMU, ControlBoardWrapper, ControlBoard_nws_ros, ControlBoard_nws_yarp, DiagnosticThread, FakeAnalogSensor, FakeBattery, FakeLaser, FakeMotionControl, FrameGrabber_nws_ros, FrameGrabber_nws_yarp, FrameTransformClient, FrameTransformGet_nwc_ros, FrameTransformGet_nws_yarp, FrameTransformServer, FrameTransformSet_nwc_ros, FrameTransformStorage, GenericSensorRosPublisher< ROS_MSG >, JoypadControlServer, JoypadControlWatchdog, LaserFromDepth, LaserFromExternalPort, LaserFromPointCloud, LaserFromRosTopic, Localization2DServer, Localization2D_nws_ros, Localization2D_nws_yarp, MonitorTrigger, MultipleAnalogSensorsServer, Nop, PriorityDebugThread, RGBDSensorWrapper, RGBDToPointCloudSensor_nws_ros, Rangefinder2DWrapper, Rangefinder2D_nws_ros, Rangefinder2D_nws_yarp, RgbdSensor_nws_ros, RgbdSensor_nws_yarp, RpLidar, RpLidar2, RpLidar3, ServerGrabber, ThreadedTimer, TimerSingleton, TransformClient, TransformServer, V4L_camera, audioFromFileDevice, fakeIMU, fakeLocalizerThread, fakeMicrophone, fakeNavigationThread, fakeSpeaker, laserHokuyo, navigation2DServer, yarp::dev::IJoypadEventDriven [private], yarp::manager::ConcurentRateWrapper, yarp::manager::YarpBroker, yarp::os::RateThread [private], yarp::os::RateThreadWrapper, and yarp::os::SystemRateThread.

Classes

class  Private
 

Public Member Functions

 PeriodicThread (double period, ShouldUseSystemClock useSystemClock=ShouldUseSystemClock::No, PeriodicThreadClock clockAccuracy=PeriodicThreadClock::Relative)
 Constructor. More...
 
 PeriodicThread (double period, PeriodicThreadClock clockAccuracy)
 Constructor. More...
 
virtual ~PeriodicThread ()
 
bool start ()
 Call this to start the thread. More...
 
void 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 () const
 Returns true when the thread is started, false otherwise. More...
 
bool isSuspended () const
 Returns true when the thread is suspended, false otherwise. More...
 
bool setPeriod (double period)
 Set the (new) period of the thread. More...
 
double getPeriod () const
 Return the current period 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 getEstimatedPeriod () const
 Return estimated period since last reset. More...
 
void getEstimatedPeriod (double &av, double &std) const
 Return estimated period since last reset. More...
 
unsigned int getIterations () const
 Return the number of iterations performed since last reset. More...
 
double getEstimatedUsed () const
 Return the estimated duration of the run() function since last reset. More...
 
void getEstimatedUsed (double &av, double &std) const
 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 () const
 Query the current priority of the thread, if the OS supports that. More...
 
int getPolicy () const
 Query the current scheduling policy of the thread, if the OS supports that. More...
 

Protected Member Functions

virtual bool threadInit ()
 Initialization method. More...
 
virtual void threadRelease ()
 Release method. More...
 
virtual void run ()=0
 Loop function. 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...
 

Detailed Description

An abstraction for a periodic thread.

Examples
os/periodicthread/periodicthread.cpp.

Definition at line 27 of file PeriodicThread.h.

Constructor & Destructor Documentation

◆ PeriodicThread() [1/2]

PeriodicThread::PeriodicThread ( double  period,
ShouldUseSystemClock  useSystemClock = ShouldUseSystemClock::No,
PeriodicThreadClock  clockAccuracy = PeriodicThreadClock::Relative 
)
explicit

Constructor.

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

Parameters
periodThe period in seconds [sec] between successive calls to the PeriodicThread::run method (remember you need to call PeriodicThread::start first before anything happens)
useSystemClockwhether the thread should always use the system clock, or depend on the current configuration of the network
clockAccuracywhether the thread should ensure all steps wake up at precise intervals using an absolute reference, otherwise compute them in a relative fashion assuming error accumulation over time due to drifts.
Warning
PeriodicThreadClock::Absolute may cause starvation if two or more busy threads lock on a common resource

Definition at line 337 of file PeriodicThread.cpp.

◆ PeriodicThread() [2/2]

PeriodicThread::PeriodicThread ( double  period,
PeriodicThreadClock  clockAccuracy 
)
explicit

Constructor.

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

Parameters
periodThe period in seconds [sec] between successive calls to the PeriodicThread::run method (remember you need to call PeriodicThread::start first before anything happens)
clockAccuracywhether the thread should ensure all steps wake up at precise intervals using an absolute reference, otherwise compute them in a relative fashion assuming error accumulation over time due to drifts
Warning
PeriodicThreadClock::Absolute may cause starvation if two or more busy threads lock on a common resource

Definition at line 342 of file PeriodicThread.cpp.

◆ ~PeriodicThread()

PeriodicThread::~PeriodicThread ( )
virtual

Definition at line 347 of file PeriodicThread.cpp.

Member Function Documentation

◆ afterStart()

void PeriodicThread::afterStart ( bool  success)
protectedvirtual

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, and yarp::os::RateThread.

Definition at line 445 of file PeriodicThread.cpp.

◆ askToStop()

void PeriodicThread::askToStop ( )

Stop the thread.

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

Definition at line 372 of file PeriodicThread.cpp.

◆ beforeStart()

void PeriodicThread::beforeStart ( )
protectedvirtual

Called just before a new thread starts.

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

Reimplemented in yarp::os::RateThreadWrapper, and yarp::os::RateThread.

Definition at line 441 of file PeriodicThread.cpp.

◆ getEstimatedPeriod() [1/2]

double PeriodicThread::getEstimatedPeriod ( ) const

Return estimated period since last reset.

Definition at line 407 of file PeriodicThread.cpp.

◆ getEstimatedPeriod() [2/2]

void PeriodicThread::getEstimatedPeriod ( double &  av,
double &  std 
) const

Return estimated period since last reset.

Parameters
[out]avaverage value
[out]stdstandard deviation

Definition at line 417 of file PeriodicThread.cpp.

◆ getEstimatedUsed() [1/2]

double PeriodicThread::getEstimatedUsed ( ) const

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

Definition at line 412 of file PeriodicThread.cpp.

◆ getEstimatedUsed() [2/2]

void PeriodicThread::getEstimatedUsed ( double &  av,
double &  std 
) const

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

Parameters
[out]avaverage value
[out]stdstandard deviation

Definition at line 422 of file PeriodicThread.cpp.

◆ getIterations()

unsigned int PeriodicThread::getIterations ( ) const

Return the number of iterations performed since last reset.

Definition at line 402 of file PeriodicThread.cpp.

◆ getPeriod()

double PeriodicThread::getPeriod ( ) const

Return the current period of the thread.

Returns
thread current period [sec].

Definition at line 357 of file PeriodicThread.cpp.

◆ getPolicy()

int PeriodicThread::getPolicy ( ) const

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

Returns
the scheduling policy of the thread.

Definition at line 460 of file PeriodicThread.cpp.

◆ getPriority()

int PeriodicThread::getPriority ( ) const

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

Returns
the priority of the thread.

Definition at line 455 of file PeriodicThread.cpp.

◆ isRunning()

bool PeriodicThread::isRunning ( ) const

Returns true when the thread is started, false otherwise.

Definition at line 387 of file PeriodicThread.cpp.

◆ isSuspended()

bool PeriodicThread::isSuspended ( ) const

Returns true when the thread is suspended, false otherwise.

Definition at line 362 of file PeriodicThread.cpp.

◆ resetStat()

void PeriodicThread::resetStat ( )

Reset thread statistics.

Definition at line 427 of file PeriodicThread.cpp.

◆ resume()

void PeriodicThread::resume ( )

Resume the thread if previously suspended.

Definition at line 397 of file PeriodicThread.cpp.

◆ run()

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

Implemented in yarp::os::RateThread, yarp::dev::IJoypadEventDriven, yarp::os::RateThreadWrapper, yarp::manager::YarpBroker, yarp::manager::ConcurentRateWrapper, TransformServer, TransformClient, Nop, ServerGrabber, FrameGrabber_nws_yarp, FrameGrabber_nws_ros, RpLidar3, RpLidar2, RpLidar, RGBDToPointCloudSensor_nws_ros, RGBDSensorWrapper, RgbdSensor_nws_yarp, RgbdSensor_nws_ros, DiagnosticThread, Rangefinder2DWrapper, Rangefinder2D_nws_yarp, Rangefinder2D_nws_ros, navigation2DServer, MultipleAnalogSensorsServer, WrenchStampedRosPublisher, TemperatureRosPublisher, PoseStampedRosPublisher, MagneticFieldRosPublisher, IMURosPublisher, GenericSensorRosPublisher< ROS_MSG >, GenericSensorRosPublisher< yarp::rosmsg::geometry_msgs::WrenchStamped >, GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::Temperature >, GenericSensorRosPublisher< yarp::rosmsg::geometry_msgs::PoseStamped >, GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::MagneticField >, GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::Imu >, Localization2DServer, Localization2D_nws_yarp, Localization2D_nws_ros, laserHokuyo, LaserFromRosTopic, LaserFromPointCloud, LaserFromExternalPort, LaserFromDepth, JoypadControlServer, JoypadControlWatchdog, BoschIMU, FrameTransformSet_nwc_ros, FrameTransformServer, FrameTransformGet_nws_yarp, FrameTransformGet_nwc_ros, FrameTransformClient, fakeNavigationThread, FakeMotionControl, fakeLocalizerThread, FakeLaser, FakeBattery, FakeAnalogSensor, ControlBoardWrapper, ControlBoard_nws_yarp, ControlBoard_nws_ros, BatteryWrapper, AudioRecorderDataThread, AudioRecorderStatusThread, AudioPlayerWrapper, AnalogWrapper, PriorityDebugThread, and MonitorTrigger.

◆ setPeriod()

bool PeriodicThread::setPeriod ( double  period)

Set the (new) period of the thread.

Parameters
periodthe period [sec]
Returns
true.

Definition at line 352 of file PeriodicThread.cpp.

◆ setPriority()

int PeriodicThread::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 450 of file PeriodicThread.cpp.

◆ start()

bool PeriodicThread::start ( )

Call this to start the thread.

Blocks until initThread() is executed.

Definition at line 382 of file PeriodicThread.cpp.

◆ step()

void PeriodicThread::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 377 of file PeriodicThread.cpp.

◆ stop()

void PeriodicThread::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 367 of file PeriodicThread.cpp.

◆ suspend()

void PeriodicThread::suspend ( )

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

Definition at line 392 of file PeriodicThread.cpp.

◆ threadInit()

bool PeriodicThread::threadInit ( )
protectedvirtual

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::IJoypadEventDriven, yarp::os::RateThreadWrapper, yarp::os::RateThread, yarp::manager::YarpBroker, TransformServer, TransformClient, ServerGrabber, FrameGrabber_nws_yarp, FrameGrabber_nws_ros, RpLidar3, RpLidar2, RpLidar, RGBDToPointCloudSensor_nws_ros, RGBDSensorWrapper, RgbdSensor_nws_yarp, RgbdSensor_nws_ros, Rangefinder2DWrapper, Rangefinder2D_nws_yarp, Rangefinder2D_nws_ros, laserHokuyo, LaserFromRosTopic, LaserFromPointCloud, LaserFromExternalPort, LaserFromDepth, JoypadControlServer, BoschIMU, FrameTransformServer, FrameTransformGet_nws_yarp, FrameTransformClient, fakeNavigationThread, FakeMotionControl, fakeLocalizerThread, FakeLaser, FakeAnalogSensor, BatteryWrapper, AudioRecorderDataThread, AudioRecorderStatusThread, AudioPlayerWrapper, AnalogWrapper, and PriorityDebugThread.

Definition at line 432 of file PeriodicThread.cpp.

◆ threadRelease()

void PeriodicThread::threadRelease ( )
protectedvirtual

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