An abstraction for a periodic thread. More...
#include <yarp/os/PeriodicThread.h>
Inherited by GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::Imu >, GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::MagneticField >, GenericSensorRosPublisher< yarp::rosmsg::geometry_msgs::PoseStamped >, GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::Temperature >, GenericSensorRosPublisher< yarp::rosmsg::geometry_msgs::WrenchStamped >, GenericSensor_nws_ros2< sensor_msgs::msg::Imu >, GenericSensor_nws_ros2< geometry_msgs::msg::WrenchStamped >, AnalogWrapper, AudioPlayerWrapper, AudioRecorderDataThread, AudioRecorderDataThread, AudioRecorderStatusThread, AudioRecorderStatusThread, Battery_nws_ros2, Battery_nws_yarp, BoschIMU, ControlBoard_nws_ros, ControlBoard_nws_ros2, ControlBoard_nws_yarp, DiagnosticThread, FakeAnalogSensor, FakeBattery, FakeLaser, FakeLaserWithMotor, FakeMotionControl, FakeMotionControlMicro, FakeOdometry2D, FakePositionSensor, FrameGrabber_nws_ros, FrameGrabber_nws_ros2, FrameGrabber_nws_yarp, FrameTransformClient, FrameTransformGet_nwc_ros, FrameTransformGet_nws_yarp, FrameTransformServer, FrameTransformSet_nwc_ros, FrameTransformSet_nwc_ros2, FrameTransformStorage, GenericSensorRosPublisher< ROS_MSG >, GenericSensor_nws_ros2< ROS_MSG >, JoypadControlServer, JoypadControlWatchdog, LaserFromDepth, LaserFromExternalPort, LaserFromPointCloud, LaserFromRosTopic, Localization2D_nws_ros, Localization2D_nws_ros2, Localization2D_nws_yarp, MonitorTrigger, MultipleAnalogSensorsServer, Nop, Odometry2D_nws_ros, Odometry2D_nws_ros2, Odometry2D_nws_yarp, PriorityDebugThread, RGBDToPointCloudSensor_nws_ros, Rangefinder2D_controlBoard_nws_ros2, Rangefinder2D_nws_ros, Rangefinder2D_nws_ros2, Rangefinder2D_nws_yarp, RgbdSensor_nws_ros, RgbdSensor_nws_ros2, RgbdSensor_nws_yarp, RgbdToPointCloudSensor_nws_ros2, Ros2Test, RpLidar, RpLidar2, RpLidar3, RpLidar4, TextureBattery, ThreadedTimer, TimerSingleton, V4L_camera, audioFromFileDevice, fakeIMU, fakeLocalizerThread, fakeMicrophone, fakeNavigation, fakeSpeaker, laserHokuyo, navigation2D_nws_yarp, yarp::dev::IJoypadEventDriven [private]
, yarp::dev::OVRHeadset, yarp::manager::ConcurentRateWrapper, and yarp::manager::YarpBroker.
Classes | |
class | Private |
Public Member Functions | |
PeriodicThread (double period, ShouldUseSystemClock useSystemClock=ShouldUseSystemClock::No, PeriodicThreadClock clockAccuracy=PeriodicThreadClock::Relative) | |
Constructor. | |
PeriodicThread (double period, PeriodicThreadClock clockAccuracy) | |
Constructor. | |
virtual | ~PeriodicThread () |
bool | start () |
Call this to start the thread. | |
void | step () |
Call this to "step" the thread rather than starting it. | |
void | stop () |
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() called). | |
void | askToStop () |
Stop the thread. | |
bool | isRunning () const |
Returns true when the thread is started, false otherwise. | |
bool | isSuspended () const |
Returns true when the thread is suspended, false otherwise. | |
bool | setPeriod (double period) |
Set the (new) period of the thread. | |
double | getPeriod () const |
Return the current period of the thread. | |
void | suspend () |
Suspend the thread, the thread keeps running by doLoop is never executed. | |
void | resume () |
Resume the thread if previously suspended. | |
void | resetStat () |
Reset thread statistics. | |
double | getEstimatedPeriod () const |
Return estimated period since last reset. | |
void | getEstimatedPeriod (double &av, double &std) const |
Return estimated period since last reset. | |
unsigned int | getIterations () const |
Return the number of iterations performed since last reset. | |
double | getEstimatedUsed () const |
Return the estimated duration of the run() function since last reset. | |
void | getEstimatedUsed (double &av, double &std) const |
Return estimated duration of the run() function since last reset. | |
int | setPriority (int priority, int policy=-1) |
Set the priority and scheduling policy of the thread, if the OS supports that. | |
int | getPriority () const |
Query the current priority of the thread, if the OS supports that. | |
int | getPolicy () const |
Query the current scheduling policy of the thread, if the OS supports that. | |
Protected Member Functions | |
virtual bool | threadInit () |
Initialization method. | |
virtual void | threadRelease () |
Release method. | |
virtual void | run ()=0 |
Loop function. | |
virtual void | beforeStart () |
Called just before a new thread starts. | |
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(). | |
An abstraction for a periodic thread.
Definition at line 26 of file PeriodicThread.h.
|
explicit |
Constructor.
Thread begins in a dormant state. Call PeriodicThread::start to get things going.
period | The period in seconds [sec] between successive calls to the PeriodicThread::run method (remember you need to call PeriodicThread::start first before anything happens) |
useSystemClock | whether the thread should always use the system clock, or depend on the current configuration of the network |
clockAccuracy | whether 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. |
Definition at line 337 of file PeriodicThread.cpp.
|
explicit |
Constructor.
Thread begins in a dormant state. Call PeriodicThread::start to get things going.
period | The period in seconds [sec] between successive calls to the PeriodicThread::run method (remember you need to call PeriodicThread::start first before anything happens) |
clockAccuracy | whether 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 |
Definition at line 342 of file PeriodicThread.cpp.
|
virtual |
Definition at line 347 of file PeriodicThread.cpp.
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 in AudioPlayerWrapper.
Definition at line 445 of file PeriodicThread.cpp.
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.
|
protectedvirtual |
Called just before a new thread starts.
This method is executed by the same thread that calls start().
Definition at line 441 of file PeriodicThread.cpp.
double PeriodicThread::getEstimatedPeriod | ( | ) | const |
Return estimated period since last reset.
Definition at line 407 of file PeriodicThread.cpp.
Return estimated period since last reset.
[out] | av | average value |
[out] | std | standard deviation |
Definition at line 417 of file PeriodicThread.cpp.
double PeriodicThread::getEstimatedUsed | ( | ) | const |
Return the estimated duration of the run() function since last reset.
Definition at line 412 of file PeriodicThread.cpp.
Return estimated duration of the run() function since last reset.
[out] | av | average value |
[out] | std | standard deviation |
Definition at line 422 of file PeriodicThread.cpp.
Return the number of iterations performed since last reset.
Definition at line 402 of file PeriodicThread.cpp.
double PeriodicThread::getPeriod | ( | ) | const |
Return the current period of the thread.
Definition at line 357 of file PeriodicThread.cpp.
int PeriodicThread::getPolicy | ( | ) | const |
Query the current scheduling policy of the thread, if the OS supports that.
Definition at line 460 of file PeriodicThread.cpp.
int PeriodicThread::getPriority | ( | ) | const |
Query the current priority of the thread, if the OS supports that.
Definition at line 455 of file PeriodicThread.cpp.
bool PeriodicThread::isRunning | ( | ) | const |
Returns true when the thread is started, false otherwise.
Definition at line 387 of file PeriodicThread.cpp.
bool PeriodicThread::isSuspended | ( | ) | const |
Returns true when the thread is suspended, false otherwise.
Definition at line 362 of file PeriodicThread.cpp.
void PeriodicThread::resetStat | ( | ) |
Reset thread statistics.
Definition at line 427 of file PeriodicThread.cpp.
void PeriodicThread::resume | ( | ) |
Resume the thread if previously suspended.
Definition at line 397 of file PeriodicThread.cpp.
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::dev::OVRHeadset, TextureBattery, RpLidar, RpLidar2, RpLidar3, RpLidar4, ControlBoard_nws_ros, FrameGrabber_nws_ros, FrameTransformGet_nwc_ros, FrameTransformSet_nwc_ros, LaserFromRosTopic, Localization2D_nws_ros, GenericSensorRosPublisher< ROS_MSG >, GenericSensorRosPublisher< yarp::rosmsg::geometry_msgs::PoseStamped >, GenericSensorRosPublisher< yarp::rosmsg::geometry_msgs::WrenchStamped >, GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::Imu >, GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::MagneticField >, GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::Temperature >, IMURosPublisher, MagneticFieldRosPublisher, PoseStampedRosPublisher, TemperatureRosPublisher, WrenchStampedRosPublisher, Odometry2D_nws_ros, Rangefinder2D_nws_ros, RgbdSensor_nws_ros, RGBDToPointCloudSensor_nws_ros, Battery_nws_ros2, ControlBoard_nws_ros2, FrameGrabber_nws_ros2, FrameTransformSet_nwc_ros2, Localization2D_nws_ros2, GenericSensor_nws_ros2< ROS_MSG >, GenericSensor_nws_ros2< geometry_msgs::msg::WrenchStamped >, GenericSensor_nws_ros2< sensor_msgs::msg::Imu >, Imu_nws_ros2, WrenchStamped_nws_ros2, Odometry2D_nws_ros2, Rangefinder2D_controlBoard_nws_ros2, Rangefinder2D_nws_ros2, RgbdSensor_nws_ros2, RgbdToPointCloudSensor_nws_ros2, Ros2Test, MonitorTrigger, PriorityDebugThread, AnalogWrapper, AudioPlayerWrapper, AudioRecorderStatusThread, AudioRecorderDataThread, AudioRecorderStatusThread, AudioRecorderDataThread, Battery_nws_yarp, ControlBoard_nws_yarp, FakeAnalogSensor, FakeBattery, FakeLaser, FakeLaserWithMotor, fakeLocalizerThread, FakeMotionControl, FakeMotionControlMicro, fakeNavigation, FakeOdometry2D, FakePositionSensor, FrameGrabber_nws_yarp, FrameTransformClient, FrameTransformGet_nws_yarp, FrameTransformServer, BoschIMU, JoypadControlWatchdog, JoypadControlServer, LaserFromDepth, LaserFromExternalPort, LaserFromPointCloud, laserHokuyo, Localization2D_nws_yarp, MultipleAnalogSensorsServer, navigation2D_nws_yarp, Odometry2D_nws_yarp, Rangefinder2D_nws_yarp, DiagnosticThread, RgbdSensor_nws_yarp, Nop, yarp::manager::ConcurentRateWrapper, yarp::manager::YarpBroker, and yarp::dev::IJoypadEventDriven.
Set the (new) period of the thread.
period | the period [sec] |
Definition at line 352 of file PeriodicThread.cpp.
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 450 of file PeriodicThread.cpp.
bool PeriodicThread::start | ( | ) |
Call this to start the thread.
Blocks until initThread() is executed.
Definition at line 382 of file PeriodicThread.cpp.
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.
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.
void PeriodicThread::suspend | ( | ) |
Suspend the thread, the thread keeps running by doLoop is never executed.
Definition at line 392 of file PeriodicThread.cpp.
|
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::OVRHeadset, RpLidar, RpLidar2, RpLidar3, RpLidar4, FrameGrabber_nws_ros, LaserFromRosTopic, Odometry2D_nws_ros, Rangefinder2D_nws_ros, RgbdSensor_nws_ros, RGBDToPointCloudSensor_nws_ros, Battery_nws_ros2, FrameGrabber_nws_ros2, Odometry2D_nws_ros2, PriorityDebugThread, AnalogWrapper, AudioPlayerWrapper, AudioRecorderStatusThread, AudioRecorderDataThread, AudioRecorderStatusThread, AudioRecorderDataThread, Battery_nws_yarp, FakeAnalogSensor, FakeLaser, FakeLaserWithMotor, fakeLocalizerThread, FakeMotionControl, FakeMotionControlMicro, fakeNavigation, FakeOdometry2D, FakePositionSensor, FrameGrabber_nws_yarp, FrameTransformClient, FrameTransformServer, BoschIMU, JoypadControlServer, LaserFromDepth, LaserFromExternalPort, LaserFromPointCloud, laserHokuyo, Localization2D_nws_yarp, Odometry2D_nws_yarp, Rangefinder2D_nws_yarp, RgbdSensor_nws_yarp, yarp::manager::YarpBroker, and yarp::dev::IJoypadEventDriven.
Definition at line 432 of file PeriodicThread.cpp.
|
protectedvirtual |
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::OVRHeadset, RpLidar, RpLidar2, RpLidar3, RpLidar4, FrameGrabber_nws_ros, LaserFromRosTopic, GenericSensorRosPublisher< ROS_MSG >, GenericSensorRosPublisher< yarp::rosmsg::geometry_msgs::PoseStamped >, GenericSensorRosPublisher< yarp::rosmsg::geometry_msgs::WrenchStamped >, GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::Imu >, GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::MagneticField >, GenericSensorRosPublisher< yarp::rosmsg::sensor_msgs::Temperature >, Odometry2D_nws_ros, Rangefinder2D_nws_ros, RgbdSensor_nws_ros, RGBDToPointCloudSensor_nws_ros, Battery_nws_ros2, FrameGrabber_nws_ros2, GenericSensor_nws_ros2< ROS_MSG >, GenericSensor_nws_ros2< geometry_msgs::msg::WrenchStamped >, GenericSensor_nws_ros2< sensor_msgs::msg::Imu >, Odometry2D_nws_ros2, PriorityDebugThread, AnalogWrapper, AudioPlayerWrapper, AudioRecorderStatusThread, AudioRecorderDataThread, AudioRecorderStatusThread, AudioRecorderDataThread, Battery_nws_yarp, FakeAnalogSensor, FakeLaser, FakeLaserWithMotor, fakeLocalizerThread, FakeMotionControl, FakeMotionControlMicro, fakeNavigation, FakeOdometry2D, FakePositionSensor, FrameTransformClient, FrameTransformServer, BoschIMU, JoypadControlServer, LaserFromDepth, LaserFromExternalPort, LaserFromPointCloud, laserHokuyo, Localization2D_nws_yarp, MultipleAnalogSensorsServer, Odometry2D_nws_yarp, Rangefinder2D_nws_yarp, RgbdSensor_nws_yarp, and yarp::manager::YarpBroker.
Definition at line 437 of file PeriodicThread.cpp.