Created
December 24, 2018 14:56
-
-
Save devendranaga/4d7385f67ba0b10d856106f19855a9fd to your computer and use it in GitHub Desktop.
Action and Actor design class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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