Skip to content

Instantly share code, notes, and snippets.

@cwoodall
Created June 24, 2016 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwoodall/5c1db855d896d4476111f273f2f631ea to your computer and use it in GitHub Desktop.
Save cwoodall/5c1db855d896d4476111f273f2f631ea to your computer and use it in GitHub Desktop.
Intersting C++ embedded process runner
#include <iostream>
#include <stdint.h>
using namespace std;
template <uint32_t f>
bool ProcessTask(void);
template <uint32_t f>
uint32_t RunProcess(uint32_t start) {
static uint32_t previous_time{start};
static uint32_t iterations{0};
while (start - previous_time >= 1000000/f) {
if (ProcessTask<f>()) {
previous_time += 1000000 / f;
++iterations;
} else {
break;
}
}
return iterations;
}
template <>
bool ProcessTask<200>(void) {
cout << "foo ";
return true;
}
template <>
bool ProcessTask<1000>(void) {
cout << "bar ";
return true;
}
template <>
bool ProcessTask<400>(void) {
cout << "meow" << endl;
return true;
}
int main(void) {
for (uint32_t i = 0; i < 1000000; i++) {
// if (i%100000) {
// cout << "t = " << i<< endl;
// }
// cout << "> Loop at t = " << i << endl;
/*cout << << "> P1 Iterations: " << */ RunProcess<1000>(i*100);
/*cout << << "> P2 Iterations: " << */ RunProcess<200>(i*100);
/*cout << << "> P3 Iterations: " << */ RunProcess<400>(i*100);
}
}
@cwoodall
Copy link
Author

cwoodall commented Jun 24, 2016

based off of flybrix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment