Skip to content

Instantly share code, notes, and snippets.

@ra1u
Last active December 10, 2016 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ra1u/6eb9b7bf9f301c2fde85d16748bd2ee8 to your computer and use it in GitHub Desktop.
Save ra1u/6eb9b7bf9f301c2fde85d16748bd2ee8 to your computer and use it in GitHub Desktop.
/*
* event_based_actor version
* it works super slow
*/
#include <string>
#include <iostream>
#include "caf/all.hpp"
using std::endl;
using std::string;
using namespace caf;
actor_system_config g_cfg;
actor_system g_system{g_cfg};
static void tree_final(event_based_actor *self, const actor &a_receiver,
int64_t n) {
self->send(a_receiver, n);
}
behavior tree(event_based_actor *self, const actor &a_receiver, int64_t d,
int64_t n) {
int counter = 10;
int64_t total = 0;
int64_t v = n * 10;
if (d == 0) {
for (int64_t i = 0; i < 10; ++i) {
g_system.spawn(tree_final, self, v + i);
}
} else {
for (int64_t i = 0; i < 10; ++i) {
g_system.spawn(tree, self, d - 1, v + i);
}
}
return behavior([=](int64_t c) mutable {
counter--;
total += c;
if (counter == 0) {
self->send(a_receiver, total);
self->quit();
}
});
}
behavior master(event_based_actor *self) {
return {[=](int64_t c) {
std::cout << "got " << c << std::endl;
self->quit();
}};
}
int main() {
actor mirror_actor = g_system.spawn(master);
const int64_t N = 5;
g_system.spawn(tree, mirror_actor, N, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment