Skip to content

Instantly share code, notes, and snippets.

@devendranaga
Created December 24, 2018 14:56
Show Gist options
  • Save devendranaga/4d7385f67ba0b10d856106f19855a9fd to your computer and use it in GitHub Desktop.
Save devendranaga/4d7385f67ba0b10d856106f19855a9fd to your computer and use it in GitHub Desktop.
Action and Actor design class
#include <iostream>
#include <thread>
#include <unistd.h>
template <typename T1, typename T2>
class action {
public:
int registerPeriodicAction(T1 *data, T2 *specificCtx)
{
d = data;
ctx = specificCtx;
}
private:
T1 *d;
T2 *ctx;
void Thread_(void)
{
while (1) {
sleep(1);
d->periodicAction(d, ctx);
}
}
std::thread *t = new std::thread(&action::Thread_, this);
};
class actor {
public:
int periodicAction(actor *actor, int *i)
{
std::cerr << "i " << *i << std::endl;
(*i) ++;
return *i;
}
};
int main()
{
int t = 0;
actor a;
action<actor, int> ac;
ac.registerPeriodicAction(&a, &t);
while (1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment