Commit a6f1b0c2 authored by Xavier Thompson's avatar Xavier Thompson

examples/: Add test program mutex.cpp

parent 3e30b044
#include <typon/typon.hpp>
#include <typon/logger.hpp>
using namespace typon;
Mutex mutex;
int N = 30;
int fibo(int n) {
if (n < 2)
return n;
return fibo(n - 1) + fibo(n - 2);
}
Task<void> with_lock(int id)
{
{
LOG("with_lock(%d), locking", id);
auto lock = mutex.lock();
co_await lock;
LOG("with_lock(%d), acquired", id);
int n = fibo(N);
LOG("with_lock(%d), fibo(%d) = %d", id, N, n);
}
LOG("with_lock(%d), released", id);
}
Join<void> parallel() {
for (int id = 0; id < 10; id++) {
co_await fork(with_lock(id));
}
}
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