Changes in / [81a05ca:7cfd6d4]


Ignore:
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
     4struct 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
     13struct 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};
    221
    322struct counter_cor {
  • doc/papers/concurrency/c++-cor/fib.cpp

    r81a05ca r7cfd6d4  
    1 #include "base.hpp"
     1#include <iostream>
     2#include <experimental/coroutine>
    23
    3 #include <algorithm>
    4 #include <iterator>
    5 #include <vector>
     4struct 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
     13struct 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};
    621
    722template<typename T>
     
    1429                }
    1530
    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(); }
    1833
    1934                void return_value(T value) {
     
    5974                return _coroutine.promise()._value;
    6075        }
    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{}; }
    9076};
    9177
     
    10086
    10187int 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;
    11892        }
    11993}
Note: See TracChangeset for help on using the changeset viewer.