powerd++
0.4.4
|
Implements an interruptible cyclic sleeping functor. More...
#include <Cycle.hpp>
Public Member Functions | |
bool | operator() () const |
Completes an interrupted sleep cycle. More... | |
template<class... DurTraits> | |
bool | operator() (std::chrono::duration< DurTraits... > const &cycleTime) |
Sleep for the time required to complete the given cycle time. More... | |
Private Types | |
using | clock = std::chrono::steady_clock |
Use steady_clock, avoid time jumps. | |
using | us = std::chrono::microseconds |
Shorthand for microseconds. | |
Private Attributes | |
std::chrono::time_point< clock > | clk = clock::now() |
The current time clock. More... | |
Implements an interruptible cyclic sleeping functor.
Cyclic sleeping means that instead of having a fixed sleeping time, each sleep is timed to meet a fixed wakeup time. I.e. the waking rhythm does not drift with changing system loads.
The canonical way to do this in C++ is like this:
The issue is that you might want to install a signal handler to guarantee stack unwinding and sleep_until() will resume its wait after the signal handler completes.
The Cycle class offers you an interruptible sleep:
In the example the while loop is terminated if the sleep()
is interrupted by a signal. Optionally the sleep cycle can be resumed:
Note there was a design decision between providing a cycle time to the constructor or providing it every cycle. The latter was chosen so the cycle time can be adjusted.
|
inline |
Completes an interrupted sleep cycle.
I.e. if the last sleep cycle was 500 ms and the sleep was interrupted 300 ms into the cycle, this would sleep for the remaining 200 ms unless interrupted.
true | Sleep completed uninterrupted |
false | Sleep was interrupted |
|
inline |
Sleep for the time required to complete the given cycle time.
I.e. if the time since the last sleep cycle was 12 ms and the given cycleTime was 500 ms, the actual sleeping time would be 488 ms.
DurTraits | The traits of the duration type |
cycleTime | The duration of the cycle to complete |
true | Command completed uninterrupted |
false | Command was interrupted |
|
private |
The current time clock.