Advertisement
rajhlinux

Untitled

Sep 4th, 2022 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | Software | 0 0
  1. // Asynchronous state machine using one CPU C++ example:
  2. // Tested working code multitasking code: [9-4-22]
  3.  
  4. #include <iostream>
  5. #include <ctime>
  6. #include <ratio>
  7. #include <chrono>
  8.  
  9. using namespace std::chrono;
  10.  
  11. // At the first execution of the program, capture the time as zero reference and store it to "t2".
  12. auto t2 = high_resolution_clock::now();
  13. auto t3 = high_resolution_clock::now();
  14.  
  15. int main()
  16. {
  17.   while (1)
  18.   {
  19.     // Always update the time reference variable "t1" to the current time:
  20.     auto t1 = high_resolution_clock::now();
  21.  
  22.     // Always check the difference of the zero reference time with the current time and see if it is greater than the set time specified in the "if" argument:
  23.     duration<double> time_span_1 = duration_cast<duration<double>>(t1 - t2);
  24.     duration<double> time_span_2 = duration_cast<duration<double>>(t1 - t3);
  25.      
  26.     if(time_span_1.count() >= 1)
  27.     {
  28.       printf("This is function_1:\n\n");
  29.       std::cout << time_span_1.count() << " Secs (t1-t2)\n\n";
  30.      
  31.       // Set t2 to capture the current time again as zero reference.
  32.       t2 = high_resolution_clock::now();
  33.      
  34.       std::cout << "------------------------------------------\n\n";
  35.     }
  36.  
  37.     else if (time_span_2.count() >= 3)
  38.     {
  39.       printf("This is function_2:\n\n");
  40.       std::cout << time_span_2.count() << " Secs (t1-t3)\n\n";
  41.      
  42.       // Set t2 to capture the current time again as zero reference.
  43.       t3 = high_resolution_clock::now();
  44.  
  45.       std::cout << "------------------------------------------\n\n";
  46.     }
  47.   }
  48.   return 0;      
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement