Skip to content

Instantly share code, notes, and snippets.

@jsiwek
Created November 6, 2014 17:11
Show Gist options
  • Save jsiwek/e43784977c2e94a55cd7 to your computer and use it in GitHub Desktop.
Save jsiwek/e43784977c2e94a55cd7 to your computer and use it in GitHub Desktop.
Testing the "after" pattern of actor-framework
#include <caf/all.hpp>
#include <unistd.h>
#include <sys/time.h>
using namespace std;
using namespace caf;
double now()
{
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec + (tv.tv_usec / 1000000.0);
}
behavior eager_actor(event_based_actor* self)
{
double creation_time = now();
return {
[=](int i)
{
aout(self) << "after " << now() - creation_time << " seconds, "
<< "got " << i << endl;
},
[](float i)
{ /* ... */ },
others() >> []
{ /* ... */ },
after(std::chrono::seconds(5)) >> [=]
{
aout(self) << "after " << now() - creation_time << " seconds, "
<< "timeout (received nothing within 5 seconds)" << endl;
}
};
}
int main()
{
auto a1 = spawn(eager_actor);
sleep(2);
anon_send(a1, 999);
await_all_actors_done();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment