Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/concurrency/examples/ProdCons.cpp

    rae6b6cf r709e0e0  
    1212struct Cons;
    1313
    14 struct resumable {
    15         virtual resumable * resume() = 0;
    16 };
    17 
    18 struct Prod : public resumable {
     14struct Prod {
    1915        struct local {
    2016                Cons * c;
     
    2420        struct promise_type {
    2521                local _l;
    26                 resumable * next;
    2722
    2823                Prod get_return_object() {
     
    7469        static Prod main();
    7570
    76         struct payment_return;
    77 
    78         payment_return payment(int money);
     71        auto payment(int money) {
     72                _coroutine.promise()._l.money = money;
     73                struct ret {
     74                        int _receipt;
     75                        bool await_ready() { return false; }
     76                        void await_suspend(std::experimental::coroutine_handle<>) {}
     77                        int await_resume() { return _receipt; }
     78                };
     79                return ret{ _coroutine.promise()._l.receipt };
     80        }
    7981
    8082        auto start(int N, Cons & c) {
     
    8284                _coroutine.promise()._l.N = N;
    8385                _coroutine.promise()._l.receipt = 0;
    84         }
    85 
    86         virtual resumable * resume() override final {
    8786                _coroutine.resume();
    88                 return _coroutine.promise().next;
    8987        }
    9088};
    9189
    92 struct Cons : public resumable {
     90struct Cons {
    9391        struct local {
    9492                Prod * p;
     
    9997        struct promise_type {
    10098                local _l;
    101                 resumable * next;
    10299
    103100                Cons get_return_object() {
     
    157154                struct ret {
    158155                        int _status;
    159                         Cons * c;
    160156                        bool await_ready() { return false; }
    161                         void await_suspend(std::experimental::coroutine_handle<Prod::promise_type> _coroutine) {
    162                                 _coroutine.promise().next = c;
    163                         }
     157                        void await_suspend(std::experimental::coroutine_handle<>) {}
    164158                        int await_resume() { return _status; }
    165159                };
    166                 return ret{ _coroutine.promise()._l.status, this };
     160                return ret{ _coroutine.promise()._l.status };
    167161        }
    168162
     
    170164                _coroutine.promise()._l.done = true;
    171165                struct ret {
    172                         Cons * c;
    173                         Prod::promise_type * _promise;
    174166                        bool await_ready() { return false; }
    175                         void await_suspend(std::experimental::coroutine_handle<Prod::promise_type> _coroutine) {
    176                                 _promise = &_coroutine.promise();
    177                                 _promise->next = c;
    178                         }
    179                         void await_resume() {
    180                                 _promise->next = nullptr;
    181                         }
     167                        void await_suspend(std::experimental::coroutine_handle<>) {}
     168                        void await_resume() {}
    182169                };
    183                 return ret{this, nullptr};
    184         }
    185 
    186         virtual resumable * resume() override final {
    187                 _coroutine.resume();
    188                 return _coroutine.promise().next;
     170                return ret{};
    189171        }
    190172};
    191 
    192 struct Prod::payment_return {
    193         int _receipt;
    194         Prod * p;
    195         bool await_ready() { return false; }
    196         void await_suspend(std::experimental::coroutine_handle<Cons::promise_type> _coroutine) {
    197                 _coroutine.promise().next = p;
    198         }
    199         int await_resume() { return _receipt; }
    200 };
    201 
    202 Prod::payment_return Prod::payment(int money)  {
    203         _coroutine.promise()._l.money = money;
    204         return payment_return{ _coroutine.promise()._l.receipt, this };
    205 }
    206173
    207174Prod Prod::main() {
     
    209176        for(int i = 0; i < p.N; i++) {
    210177                int p1 = random(100), p2 = random(100);
    211                 std::cout << p1 << " " << p2 << std::endl;
     178                std::cout << p1 << " " << p2;
    212179                int status = co_await p.c->deliver(p1, p2);
    213                 std::cout << " $" << p.money << std::endl << status << std::endl;
     180                std::cout << " $" << p.money << std::endl << status;
    214181                p.receipt += 1;
    215182        }
    216183        co_await p.c->stop();
    217         std::cout << "prod stops" << std::endl;
     184        std::cout << "prod stops";
    218185}
    219186
     
    222189        int money = 1, receipt;
    223190        for(;!c.done ;) {
    224                 std::cout << c.p1 << " " << c.p2 << std::endl;
    225                 std::cout << " $ " << money << std::endl;
     191                std::cout << c.p1 << " " << c.p2 << std::endl << " $" << money;
    226192                c.status += 1;
    227193                receipt = co_await c.p->payment( money );
    228                 std::cout << " # " << receipt << std::endl;
     194                std::cout << " #" << receipt;
    229195                money += 1;
    230196        }
    231         std::cout << "cons stops" << std::endl;
    232 }
    233 
    234 void dispatch(resumable * r) {
    235         while((r = r->resume()));
     197        std::cout << "const stops";
    236198}
    237199
     
    241203        srandom( getpid() );
    242204        prod.start(5, cons);
    243         dispatch(&prod);
    244 }
     205}
Note: See TracChangeset for help on using the changeset viewer.