Skip to content

Instantly share code, notes, and snippets.

@jsiwek
Created November 5, 2014 20:20
Show Gist options
  • Save jsiwek/e738fce60ef57bc5a315 to your computer and use it in GitHub Desktop.
Save jsiwek/e738fce60ef57bc5a315 to your computer and use it in GitHub Desktop.
Example scoped_actor usage from actor-framework.
#include <caf/all.hpp>
#include <unistd.h>
using namespace std;
using namespace caf;
struct foo {
actor a;
~foo()
{
cout << "~foo start" << endl;
scoped_actor self;
self->sync_send(a, atom("shutdown")).await(
on(atom("ok")) >> [=] {}
);
cout << "~foo end" << endl;
}
};
behavior my_actor(event_based_actor* self)
{
return {
on(atom("work")) >> [=]
{
sleep(1);
},
on(atom("shutdown")) >> [=]
{
self->quit();
return atom("ok");
}
};
}
int main()
{
auto f = new foo{spawn(my_actor)};
anon_send(f->a, atom("work"));
anon_send(f->a, atom("work"));
anon_send(f->a, atom("work"));
delete f;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment