Commit da6a75a1 authored by Xavier Thompson's avatar Xavier Thompson

Add future.cpp test program

parent 2f37b314
#include <typon/typon.hpp>
#include <cstdio>
using namespace typon;
int fibo(int n) {
if (n < 2)
return n;
return fibo(n - 1) + fibo(n - 2);
}
Task<void> consume(Future<int> & f) {
printf("[%u] waiting on future\n", Scheduler::thread_id);
int value = co_await f;
printf("[%u] future yielded %d\n", Scheduler::thread_id, value);
}
Task<void> produce(Future<int> & f, int n) {
int value = fibo(n);
printf("[%u] producing %d\n", Scheduler::thread_id, value);
f.put(value);
co_return;
}
Join<void> parallel() {
Future<int> f;
printf("[%u] fork(consume())\n", Scheduler::thread_id);
co_await fork(consume(f));
printf("[%u] fork(produce())\n", Scheduler::thread_id);
co_await fork(produce(f, 20));
}
Root root() {
co_await parallel();
}
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