coroutine.hpp 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. #ifndef __COROUTINE_HPP__
  2. #define __COROUTINE_HPP__
  3. #include <vector>
  4. #include <boost/coroutine2/coroutine.hpp>
  5. typedef boost::coroutines2::coroutine<void>::pull_type coro_t;
  6. typedef boost::coroutines2::coroutine<void>::push_type yield_t;
  7. inline void run_coroutines(MPCIO &mpcio, std::vector<coro_t> &coroutines) {
  8. // Loop until all the coroutines are finished
  9. bool finished = false;
  10. while(!finished) {
  11. // If this current function is not itself a coroutine (i.e.,
  12. // this is the top-level function that launches all the
  13. // coroutines), here's where to call send(). Otherwise, call
  14. // yield() here to let other coroutines at this level run.
  15. mpcio.sendall();
  16. finished = true;
  17. for (auto &c : coroutines) {
  18. // This tests if coroutine c still has work to do (is not
  19. // finished)
  20. if (c) {
  21. finished = false;
  22. // Resume coroutine c from the point it yield()ed
  23. c();
  24. }
  25. }
  26. }
  27. }
  28. #endif