Commit 6004d476 authored by Xavier Thompson's avatar Xavier Thompson

examples/: Add programs to test fork policies

parent 8e6211d3
#include <typon/typon.hpp>
#include <cstdio>
using namespace typon;
using namespace typon::policy;
Join<int> fibo(int n) {
if (n < 2) {
co_return n;
}
Forked a = co_await fork<Bundle>(fibo(n - 1));
Forked b = co_await fork<Bundle>(fibo(n - 2));
co_await Sync();
co_return a.get() + b.get();
}
Root root() {
int result = co_await fibo(40);
printf("%d\n", result);
}
int main() {
root().call();
puts("done");
return 0;
}
#include <typon/typon.hpp>
#include <cstdio>
using namespace typon;
using namespace typon::policy;
Join<int> fibo(int n, int & result) {
if (n < 2) {
result = n;
co_return n;
}
// Danger !
// The memory for a and b could be freed before the forks complete
int a; co_await fork<Drop>(fibo(n - 1, a));
int b; co_await fork<Drop>(fibo(n - 2, b));
// This sync() ensures that the forks complete before a and b are freed
// As long as there are no exceptions...
co_await Sync();
result = a + b;
}
Root root() {
int result; co_await fibo(40, result);
printf("%d\n", result);
}
int main() {
root().call();
puts("done");
return 0;
}
#include <typon/typon.hpp>
#include <cstdio>
using namespace typon;
using namespace typon::policy;
Join<int> fibo(int n) {
if (n < 2) {
co_return n;
}
Forked a = co_await fork<Refcnt>(fibo(n - 1));
Forked b = co_await fork<Refcnt>(fibo(n - 2));
co_await Sync();
co_return a.get() + b.get();
}
Root root() {
int result = co_await fibo(40);
printf("%d\n", result);
}
int main() {
root().call();
puts("done");
return 0;
}
#include <typon/typon.hpp>
#include <cstdio>
using namespace typon;
Join<void> fibo(int n, int & result) {
if (n < 2) {
result = n;
co_return;
}
// Danger !
// The memory for a and b could be freed before the forks complete
int a; co_await fork(fibo(n - 1, a));
int b; co_await fork(fibo(n - 2, b));
// This sync() ensures that the forks complete before a and b are freed
// As long as there are no exceptions...
co_await Sync();
result = a + b;
}
Root root() {
int result; co_await fibo(40, result);
printf("%d\n", result);
}
int main() {
root().call();
puts("done");
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment