Changes in / [81a05ca:7cfd6d4]
- Location:
- doc/papers/concurrency/c++-cor
- Files:
-
- 3 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/concurrency/c++-cor/counter.cpp
r81a05ca r7cfd6d4 1 #include "base.hpp" 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 }; 2 21 3 22 struct counter_cor { -
doc/papers/concurrency/c++-cor/fib.cpp
r81a05ca r7cfd6d4 1 #include "base.hpp" 1 #include <iostream> 2 #include <experimental/coroutine> 2 3 3 #include <algorithm> 4 #include <iterator> 5 #include <vector> 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 }; 6 21 7 22 template<typename T> … … 14 29 } 15 30 16 auto initial_suspend() { return suspend_ always(); }17 auto final_suspend() { return suspend_ always(); }31 auto initial_suspend() { return suspend_never(); } 32 auto final_suspend() { return suspend_never(); } 18 33 19 34 void return_value(T value) { … … 59 74 return _coroutine.promise()._value; 60 75 } 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{}; }90 76 }; 91 77 … … 100 86 101 87 int main() { 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 } 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; 118 92 } 119 93 }
Note: See TracChangeset
for help on using the changeset viewer.