- Timestamp:
- Mar 19, 2019, 3:09:42 PM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 81a05ca, 8f936bf
- Parents:
- f1c1339
- Location:
- doc/papers/concurrency/c++-cor
- Files:
-
- 3 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/concurrency/c++-cor/counter.cpp
rf1c1339 rbe3416d 1 #include <iostream> 2 #include <experimental/coroutine> 3 4 struct suspend_never { 5 bool await_ready() noexcept { 6 return true; 7 } 8 9 void await_suspend(std::experimental::coroutine_handle<>) noexcept {} 10 void await_resume() noexcept {} 11 }; 12 13 struct suspend_always { 14 bool await_ready() noexcept { 15 return false; 16 } 17 18 void await_suspend(std::experimental::coroutine_handle<>) noexcept {} 19 void await_resume() noexcept {} 20 }; 1 #include "base.hpp" 21 2 22 3 struct counter_cor { -
doc/papers/concurrency/c++-cor/fib.cpp
rf1c1339 rbe3416d 1 #include <iostream> 2 #include <experimental/coroutine> 1 #include "base.hpp" 3 2 4 struct suspend_never { 5 bool await_ready() noexcept { 6 return true; 7 } 8 9 void await_suspend(std::experimental::coroutine_handle<>) noexcept {} 10 void await_resume() noexcept {} 11 }; 12 13 struct suspend_always { 14 bool await_ready() noexcept { 15 return false; 16 } 17 18 void await_suspend(std::experimental::coroutine_handle<>) noexcept {} 19 void await_resume() noexcept {} 20 }; 3 #include <algorithm> 4 #include <iterator> 5 #include <vector> 21 6 22 7 template<typename T> … … 29 14 } 30 15 31 auto initial_suspend() { return suspend_ never(); }32 auto final_suspend() { return suspend_ never(); }16 auto initial_suspend() { return suspend_always(); } 17 auto final_suspend() { return suspend_always(); } 33 18 34 19 void return_value(T value) { … … 74 59 return _coroutine.promise()._value; 75 60 } 61 62 struct iterator : std::iterator<std::input_iterator_tag, T> { 63 std::experimental::coroutine_handle<promise_type> _coroutine = nullptr; 64 65 iterator() = default; 66 explicit iterator(std::experimental::coroutine_handle<promise_type> coroutine) 67 : _coroutine(coroutine) 68 {} 69 70 iterator& operator++() { 71 _coroutine.resume(); 72 return *this; 73 } 74 75 T const & operator*() const { 76 return _coroutine.promise()._value; 77 } 78 }; 79 80 iterator begin() { 81 if(_coroutine) { 82 _coroutine.resume(); 83 if(_coroutine.done()) { return end(); } 84 } 85 86 return iterator{ _coroutine }; 87 } 88 89 iterator end() { return iterator{}; } 76 90 }; 77 91 … … 86 100 87 101 int main() { 88 auto f1 = fib(); 89 auto f2 = fib(); 90 for(int i = 0; i < 10; i++) { 91 std::cout << f1.next() << " " << f2.next() << std::endl; 102 { 103 auto f1 = fib(); 104 auto f2 = fib(); 105 for(int i = 0; i < 10; i++) { 106 std::cout << f1.next() << " " << f2.next() << std::endl; 107 } 108 } 109 110 { 111 auto f1 = fib(); 112 std::vector<int> fibs; 113 std::copy_n(f1.begin(), 10, std::back_inserter(fibs)); 114 115 for(auto i : fibs) { 116 std::cout << i << std::endl; 117 } 92 118 } 93 119 }
Note: See TracChangeset
for help on using the changeset viewer.