clean up concurrency example

master
Daniel Kolesa 2017-03-19 16:38:30 +01:00
parent 9a9466e943
commit 16362167e7
1 changed files with 19 additions and 30 deletions

View File

@ -3,38 +3,31 @@
using namespace ostd; using namespace ostd;
template<typename S>
void foo(S &sched) {
auto arr = ostd::iter({ 150, 38, 76, 25, 67, 18, -15, -38, 25, -10 });
auto h1 = arr.slice(0, arr.size() / 2);
auto h2 = arr.slice(arr.size() / 2, arr.size());
auto c = sched.template make_channel<int>();
auto f = [](auto c, auto half) {
c.put(foldl(half, 0));
};
sched.spawn(f, c, h1);
sched.spawn(f, c, h2);
int a, b;
c.get(a);
c.get(b);
writefln("first half: %s", a);
writefln("second half: %s", b);
writefln("total: %s", a + b);
}
int main() { int main() {
auto foo = [](auto &sched) {
auto arr = ostd::iter({ 150, 38, 76, 25, 67, 18, -15, 215, 25, -10 });
auto c = sched.template make_channel<int>();
auto f = [](auto c, auto half) {
c.put(foldl(half, 0));
};
sched.spawn(f, c, arr.slice(0, arr.size() / 2));
sched.spawn(f, c, arr.slice(arr.size() / 2, arr.size()));
int a, b;
c.get(a), c.get(b);
writefln("%s + %s = %s", a, b, a + b);
};
thread_scheduler tsched; thread_scheduler tsched;
tsched.start([&tsched]() { tsched.start([&tsched, &foo]() {
writeln("thread scheduler: starting..."); writeln("thread scheduler: starting...");
foo(tsched); foo(tsched);
writeln("thread scheduler: finishing..."); writeln("thread scheduler: finishing...");
}); });
simple_coroutine_scheduler scsched; simple_coroutine_scheduler scsched;
scsched.start([&scsched]() { scsched.start([&scsched, &foo]() {
writeln("simple coroutine scheduler: starting..."); writeln("simple coroutine scheduler: starting...");
foo(scsched); foo(scsched);
writeln("simple coroutine scheduler: finishing..."); writeln("simple coroutine scheduler: finishing...");
@ -43,13 +36,9 @@ int main() {
/* /*
thread scheduler: starting... thread scheduler: starting...
first half: 356 356 + 233 = 589
second half: -20
total: 336
thread scheduler: finishing... thread scheduler: finishing...
simple coroutine scheduler: starting... simple coroutine scheduler: starting...
first half: 356 356 + 233 = 589
second half: -20
total: 336
simple coroutine scheduler: finishing... simple coroutine scheduler: finishing...
*/ */