diff --git a/examples/concurrency.cc b/examples/concurrency.cc index 500bdf0..ae6c723 100644 --- a/examples/concurrency.cc +++ b/examples/concurrency.cc @@ -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); }; diff --git a/ostd/channel.hh b/ostd/channel.hh index 11fe045..f2f4a32 100644 --- a/ostd/channel.hh +++ b/ostd/channel.hh @@ -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; }