Last active
December 10, 2016 21:01
-
-
Save ra1u/6eb9b7bf9f301c2fde85d16748bd2ee8 to your computer and use it in GitHub Desktop.
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
/* | |
* 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