Changeset 81a05ca for doc/papers


Ignore:
Timestamp:
Mar 19, 2019, 4:07:18 PM (5 years ago)
Author:
Peter A. Buhr <pabuhr@…>
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:
79b018f3
Parents:
7cfd6d4 (diff), be3416d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
doc/papers/concurrency/c++-cor
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/concurrency/c++-cor/counter.cpp

    r7cfd6d4 r81a05ca  
    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"
    212
    223struct counter_cor {
  • doc/papers/concurrency/c++-cor/fib.cpp

    r7cfd6d4 r81a05ca  
    1 #include <iostream>
    2 #include <experimental/coroutine>
     1#include "base.hpp"
    32
    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>
    216
    227template<typename T>
     
    2914                }
    3015
    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(); }
    3318
    3419                void return_value(T value) {
     
    7459                return _coroutine.promise()._value;
    7560        }
     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{}; }
    7690};
    7791
     
    86100
    87101int 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                }
    92118        }
    93119}
Note: See TracChangeset for help on using the changeset viewer.