Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Asynchronous state machine using one CPU C++ example:
- // Tested working code multitasking code: [9-4-22]
- #include <iostream>
- #include <ctime>
- #include <ratio>
- #include <chrono>
- using namespace std::chrono;
- // At the first execution of the program, capture the time as zero reference and store it to "t2".
- auto t2 = high_resolution_clock::now();
- auto t3 = high_resolution_clock::now();
- int main()
- {
- while (1)
- {
- // Always update the time reference variable "t1" to the current time:
- auto t1 = high_resolution_clock::now();
- // 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:
- duration<double> time_span_1 = duration_cast<duration<double>>(t1 - t2);
- duration<double> time_span_2 = duration_cast<duration<double>>(t1 - t3);
- if(time_span_1.count() >= 1)
- {
- printf("This is function_1:\n\n");
- std::cout << time_span_1.count() << " Secs (t1-t2)\n\n";
- // Set t2 to capture the current time again as zero reference.
- t2 = high_resolution_clock::now();
- std::cout << "------------------------------------------\n\n";
- }
- else if (time_span_2.count() >= 3)
- {
- printf("This is function_2:\n\n");
- std::cout << time_span_2.count() << " Secs (t1-t3)\n\n";
- // Set t2 to capture the current time again as zero reference.
- t3 = high_resolution_clock::now();
- std::cout << "------------------------------------------\n\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement