Skip to content

Instantly share code, notes, and snippets.

@film42
Last active January 17, 2019 03:23
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 film42/3a5944d6b1b3afa8ba64059d2fbe8cb3 to your computer and use it in GitHub Desktop.
Save film42/3a5944d6b1b3afa8ba64059d2fbe8cb3 to your computer and use it in GitHub Desktop.
Simple example using foundationdb's Flow
#include <iostream>
#include <vector>
#include "flow/flow.h"
#include "flow/DeterministicRandom.h"
#include "flow/actorcompiler.h"
// Simple counting actor that:
// 1. Logs a tag on start and finish.
// 2. Waits for some time.
// 3. Returns the new number.
ACTOR Future<int> asyncAdd(std::string tag, Future<int> f, int offset) {
std::cout << "Message received: " << tag << std::endl;
int value = wait(delayed(f, 0.5));
std::cout << "Message delay complete: " << tag << std::endl;
return value + offset;
}
// Set up globals: network and random function.
void prelude() {
g_nondeterministic_random = new DeterministicRandom(1);
g_network = newNet2(NetworkAddress(), true, false);
}
int main() {
prelude();
// We compose our future chain.
auto result1 = asyncAdd("r1-m3", asyncAdd("r1-m2", asyncAdd("r1-m1", Future<int>(10), 1), 20), 30);
auto result2 = asyncAdd("r2-m3", asyncAdd("r2-m2", asyncAdd("r2-m1", Future<int>(10), 5), 30), 40);
// Tell the network when it can stop: wait for both future chains to complete.
auto r = stopAfter(waitForAll(std::vector<Future<int>>({result1, result2})));
if (r.isError()) {
std::cout << "Something bad happened: " << r.getError().what() << std::endl;
}
// Start the network and wait for it to stop.
g_network->run();
// Check and print the results.
if (result1.isReady() && result2.isReady()) {
std::cout << "Result 1: " << result1.getValue() << std::endl;
std::cout << "Result 2: " << result2.getValue() << std::endl;
} else {
std::cout << "Failed!" << std::endl;
return 1;
}
}
$ mono ../../foundationdb/building/actorcompiler.exe Counting.actor.cpp Counting.actor.out.cpp && g++ -g -DNO_INTELLISENSE -DTLS_DISABLED -I/home/film42/Development/Fun/foundationdb -I/home/film42/Development/Fun/foundationdb/building -I/home/film42/Development/Fun/foundationdb/building/flow -I/home/film42/Development/Fun/foundationdb/building/boostProject-prefix/src/boostProject -lrt -ldl -lpthread -std=c++11 -o counting Counting.actor.out.cpp /home/film42/Development/Fun/foundationdb/building/lib/libflow.a && ./counting
actorcompiler Counting.actor.cpp Counting.actor.out.cpp
Compiled ACTOR asyncAdd (line 11)
Message received: r1-m1
Message received: r1-m2
Message received: r1-m3
Message received: r2-m1
Message received: r2-m2
Message received: r2-m3
Message delay complete: r1-m1
Message delay complete: r2-m1
Message delay complete: r1-m2
Message delay complete: r2-m2
Message delay complete: r1-m3
Message delay complete: r2-m3
Result 1: 61
Result 2: 85
./counting 0.00s user 0.00s system 0% cpu 1.503 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment