qimessaging provides numerous ways of doing async operations, but sometimes you
need an async operation which repeats itself. This is what you can do with
qi::PeriodicTask
.
Global Classes
class qi::PeriodicTask
Functions (class qi::PeriodicTask)
#include <qi/periodictask.hpp>
void printDate();
qi::PeriodicTask task;
task.setName("print date");
task.setUsPeriod(1000*1000);
task.setCallback(printDate);
task.start();
// and at the end
task.stop();
This will create a task which will call printDate every second. The name of the
task is not used yet, but may be useful to provide debug information later.
start()
will call the task immediately (unless given false as argument) and
call it again every second.
In all cases, PeriodicTask
never gives hard time guaranties (like realtime).
If there is a lot of work in the event loop, your task may be called later than
expected.
However, PeriodicTask
guaranties you that it will never call your callback
twice at the same time, even if it missed it’s deadline. Also, when you call
stop, it will block until the callback is finished if it’s currently running.
Sometime, your callback may be slow and you may wonder what happens in these cases.
Here is a simple task with a 5s period and a 3s callback:
v v
+-----------+ +-----------
+ Task 3s + wait 5s + Task 3s ...
+-----------+--------------------+-----------
Same thing with callback compensation enabled:
v v
+-----------+ +-----------
+ Task 3s + wait 2s + Task 3s ...
+-----------+---------+-----------
Same thing again with a slower task:
v v
+----------------------------+---------------------------+----
+ Task 7s + Task 7s + ...
+----------------------------+---------------------------+----
The task will never be called twice even if it takes longer than the period.
Note that it is not recommended to use a periodic task with slow tasks (as other qimessaging’s async methods).
Control a task executed periodically and asynchronously. . More...
#include <qi/periodictask.hpp>
noncopyable
setCallback
(T&& cb)setCallback
(T&& cb)
()setStrand
(qi::Strand* strand)setUsPeriod
(qi::int64_t usPeriod)setPeriod
(qi::Duration period)start
(bool immediate)trigger
()stop
()asyncStop
()compensateCallbackTime
(bool compensate)setName
(const std::string& name)isRunning
() constisStopping
() constPeriodicTask
()~PeriodicTask
()Control a task executed periodically and asynchronously. .
qi::PeriodicTask::
setCallback
(T&& cb)¶qi::PeriodicTask::
setCallback
(T&& cb)()
qi::PeriodicTask::
setStrand
(qi::Strand* strand)¶Set the strand on which to schedule the calls
qi::PeriodicTask::
setUsPeriod
(qi::int64_t usPeriod)¶Deprecatedsince 2.3. Use setPeriod.
qi::PeriodicTask::
setPeriod
(qi::Duration period)¶Brief: Set the call interval.
Parameters: |
|
---|
This call will wait until next callback invocation to apply the change. If you call this function from within the callback, it will be taken into account immediately.
qi::PeriodicTask::
start
(bool immediate = true)¶Brief: Start the periodic task at specified period.
Parameters: |
|
---|
No effect if already running. No effect if called from within the callback.
qi::PeriodicTask::
trigger
()¶Trigger a started periodic task to run right now. Does nothing if the periodic task just ran, is running, starting, stopping or stopped. This function is lockfree.
qi::PeriodicTask::
stop
()¶When this function returns, the callback will not be called anymore. Can be called from within the callback function.
qi::PeriodicTask::
asyncStop
()¶Request for periodic task to stop asynchronously. Can be safely called from within the callback.
qi::PeriodicTask::
compensateCallbackTime
(bool compensate)¶If argument is true, call interval will take into account call duration to maintain the period.
qi::PeriodicTask::
setName
(const std::string& name)¶Brief: Set name for debugging and tracking purpose.
Parameters: |
|
---|
qi::PeriodicTask::
isRunning
() const
¶Brief:
Returns: | true if task is running |
---|
qi::PeriodicTask::
isStopping
() const
¶Brief:
Returns: | whether state is stopping or stopped |
---|
Can be called from within the callback to know if stop() or asyncStop() was called.
qi::PeriodicTask::
PeriodicTask
()¶Default constructor.
qi::PeriodicTask::
~PeriodicTask
()¶Default destructor.