SHARE
TWEET

Periodic Processing With Standard C++11 Facilities

a guest Jun 5th, 2018 366 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include   <chrono>
  2. #include   <iostream>
  3. #include   <thread>
  4. #include   <vector>
  5. #include   <numeric>
  6.  
  7. using namespace std::chrono;
  8.  
  9.  
  10.  
  11.  
  12.  
  13. typedef system_clock testclock;
  14. //typedef high_resolution_clock testclock;
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. //Determine our platform's tic period
  22. const double microsPerClkTic{
  23.     1.0E6 * testclock::period::num / testclock::period::den };
  24.  
  25. //Our REQUIRED processing period
  26. const milliseconds intervalPeriodMillis{ 10 };
  27.  
  28. //The number of periods we'll run our test fixture for
  29. const int32_t numLoops{ 3000 + 1 };
  30.  
  31. void periodicTask() {
  32.    
  33.     std::cout <<   "clock precision = "
  34.     <<   microsPerClkTic
  35.     <<   " microseconds/tic"
  36.     <<   std::endl
  37.     <<   "Desired Wakeup Period = "
  38.     <<   intervalPeriodMillis.count()
  39.     <<   " milliseconds"
  40.     <<   std::endl
  41.     <<   "Num Wakeups = "
  42.     <<   numLoops - 1
  43.     <<   std::endl;
  44.    
  45.     //Initialize the chrono timepoint & duration objects we'll be
  46.     //using over & over inside our sleep loop
  47.     testclock::time_point currentStartTime{ testclock::now() };
  48.     testclock::time_point nextStartTime{ currentStartTime };
  49.    
  50.     //For storing our errors
  51.     std::vector<testclock::duration> wakeupErrors{};
  52.     wakeupErrors.reserve(numLoops);
  53.    
  54.     int32_t loopNum{}; //loop counter
  55.    
  56.     //Let's ROCK!!!!!!
  57.     while (loopNum < numLoops) {
  58.        
  59.         //Get our current "wakeup" time
  60.         currentStartTime = testclock::now();
  61.        
  62.         //Store the wakeup error for this pass
  63.         wakeupErrors.push_back(currentStartTime - nextStartTime);
  64.        
  65.         //Determine the point in time at which we want to wakeup for the
  66.             //next pass through the loop.
  67.         nextStartTime =
  68.         currentStartTime + intervalPeriodMillis;
  69.        
  70.         //Sleep till our next period start time
  71.         std::this_thread::sleep_until(nextStartTime);
  72.        
  73.         ++loopNum;
  74.        
  75.     } //end while
  76.    
  77.     //Print the average wakeup error
  78.     testclock::duration sum{};
  79.     sum = std::accumulate(std::begin(wakeupErrors) + 1,
  80.                           std::end(wakeupErrors),
  81.                           sum);
  82.    
  83.     std::cout << "\nAverage Wakeup Error = "
  84.     << duration_cast<microseconds>(sum).count() / (numLoops - 1)
  85.     << " microseconds\n";
  86.    
  87. }
  88.  
  89. int main() {
  90.    
  91.     periodicTask();
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top