channel api cleanup

master
Daniel Kolesa 2017-03-19 19:35:00 +01:00
parent 94d72b693e
commit d35e8b6341
2 changed files with 11 additions and 6 deletions

View File

@ -12,10 +12,9 @@ int main() {
c.put(foldl(half, 0));
};
spawn(sched, f, c, arr.slice(0, arr.size() / 2));
spawn(sched, f, c, arr.slice(arr.size() / 2, arr.size()));
spawn(sched, f, c, arr + (arr.size() / 2));
int a, b;
c.get(a), c.get(b);
int a = c.get(), b = c.get();
writefln("%s + %s = %s", a, b, a + b);
};

View File

@ -46,8 +46,11 @@ struct channel {
p_state->put(std::move(val));
}
bool get(T &val) {
return p_state->get(val, true);
T get() {
T ret;
/* guaranteed to return true if at all */
p_state->get(ret, true);
return ret;
}
bool try_get(T &val) {
@ -87,9 +90,12 @@ private:
}
}
if (p_messages.empty()) {
if (p_closed) {
throw channel_error{"get from a closed channel"};
}
return false;
}
val = p_messages.front();
val = std::move(p_messages.front());
p_messages.pop_front();
return true;
}