Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.