some universal funcs for working with schedulers

master
Daniel Kolesa 2017-03-19 18:12:08 +01:00
parent 16362167e7
commit c123e98178
2 changed files with 24 additions and 3 deletions

View File

@ -7,12 +7,12 @@ 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 c = make_channel<int>(sched);
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()));
spawn(sched, f, c, arr.slice(0, arr.size() / 2));
spawn(sched, f, c, arr.slice(arr.size() / 2, arr.size()));
int a, b;
c.get(a), c.get(b);

View File

@ -16,6 +16,9 @@
namespace ostd {
struct thread_scheduler {
template<typename T>
using channel_type = channel<T>;
~thread_scheduler() {
join_all();
}
@ -118,6 +121,9 @@ private:
};
public:
template<typename T>
using channel_type = channel<T, coro_cond>;
template<typename F, typename ...A>
auto start(F &&func, A &&...args) -> std::result_of_t<F(A...)> {
using R = std::result_of_t<F(A...)>;
@ -188,6 +194,21 @@ private:
typename std::list<coro>::iterator p_idx = p_coros.end();
};
template<typename S, typename F, typename ...A>
inline void spawn(S &sched, F &&func, A &&...args) {
sched.spawn(std::forward<F>(func), std::forward<A>(args)...);
}
template<typename S>
inline void yield(S &sched) {
sched.yield();
}
template<typename T, typename S>
inline auto make_channel(S &sched) -> typename S::template channel_type<T> {
return sched.template make_channel<T>();
}
} /* namespace ostd */
#endif